Laravel - Foreach loop from a language file?

- 1 answer

Ad

have a language file called "faq" which lists all the frequently asked questions and answers.

I have no idea how to do a foreach loop from those language files.

My faq.php language file:

return [
    'faq_1'       => 'Question here',
    'faq_1_ans'   => 'Answer here',
    'faq_2'       => 'Question here',
    'faq_2_ans'   => 'Answer here',
];

How can I make that into a foreach loop? I really don't know where to start.

Ad

Answer

Ad

You might want to change the structure

return [
    'faq_1'       => [
        'q' => "Question",
        'a' => "Answer Here"
    ],
    'faq_2'       => [
        'q' => "Question",
        'a' => "Answer Here"
    ],
];

or even

return [
    [
        'q' => "Question",
        'a' => "Answer Here"
    ],
    [
        'q' => "Question",
        'a' => "Answer Here"
    ],
];

this way you can loop:

$faqs = Lang::get('faq');
foreach($faqs as $faq)
{
    echo "question: " . $faq['q'];
    echo "answer: " . $faq['a']:
} 
Ad
source: stackoverflow.com
Ad