Web Student
Web Student

Reputation: 735

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?

Upvotes: 0

Views: 1380

Answers (2)

Muhammad Nauman Yousaf
Muhammad Nauman Yousaf

Reputation: 147

Alternatively, you can use my helper function.

function printPre($array, $string = false, $exit = true){

if(env('APP_ENV')=='local') {

    if($string){
        print_r($array, $string);
    }
    else{
        echo '<pre>';
        print_r($array);
        echo '</pre>';
    }
    if($exit)
        exit;
}

}

Upvotes: 0

trq
trq

Reputation: 146

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.

Upvotes: 2

Related Questions