Search code examples
phpforeachlaravellaravel-4iteration

Laravel foreach only returns first value


First please be gentle i am a beginner and only prectising,

I have a problem, i would like to get laravel's language files and edit their content.

My problem is what i dont really understand is the following

i have a functions what returns the actual files, i have a variable what stores this

$directory =  File::files(self::$lang_path.$code);

id i die and dump i get back the following

array(3) {
  [0]=>
  string(26) "app/lang/en/pagination.php"
  [1]=>
  string(25) "app/lang/en/reminders.php"
  [2]=>
  string(26) "app/lang/en/validation.php"
}

all fine, but if foreach it and die and dump

$directory =  File::files(self::$lang_path.$code);

    foreach ($directory as $files) 
    {
        dd($files);

    }

i just get back string(26) "app/lang/en/pagination.php"

could please tell me what i am doing wrong?

and the problem is i need it because i will need nested foreac

like

$directory =  File::files(self::$lang_path.$code);

        foreach ($directory as $files) 
        {
            foreach ($files as $file) 
            {
                // preform more stuff
            }

        }

and idont understand what iam doing wrong, could please someone give me a hint?


Solution

  • The dd (dump and die) function kills script execution - It therefore will only show the first item in the array before calling die().

    Use var_dump instead.

    If you're curious, you can see the function definition here.