Home > Back-end >  after array crunch i get array likes this [[10,9,7],[6,5,4],[3,1]] , but i want to foreach loop for
after array crunch i get array likes this [[10,9,7],[6,5,4],[3,1]] , but i want to foreach loop for

Time:01-21

Here is my code , i want foreach loop for all element of arrays

 $all_users  = \App\User::orderBy('id','desc')->pluck('id')->toArray();
               $all_user = array_chunk($all_users, 3);
             foreach($all_user as $numbers){
                 foreach($numbers as $number){                   
                    return $all_firebase_tokens = \App\UserDeviceId::WhereIn('user_id',$number)->pluck('firebase_token')->toArray();
                }
            }

CodePudding user response:

As Jaquarh mentioned, the very first iteration of the foreach loop will stop the whole function by already returning a value. Assuming what you need is an array of the firebase_tokens, something like this is likely what you need:

$all_users  = \App\User::orderBy('id','desc')->pluck('id')->toArray();
$all_user = array_chunk($all_users, 3);
$all_firebase_tokens = [];
foreach($all_user as $numbers){
    foreach($numbers as $number){                   
        array_push($all_firebase_tokens, \App\UserDeviceId::WhereIn('user_id',[$number])->pluck('firebase_token')->toArray());
    }
}
return $all_firebase_tokens;

CodePudding user response:

Simple and small code snippet, you can easily loop on $arraySingle :

$array = [[10,9,7],[6,5,4],[3,1]];

$arraySingle = call_user_func_array('array_merge', $array);

print_r($arraySingle);

Output will be like this:

Array ( [0] => 10 [1] => 9 [2] => 7 [3] => 6 [4] => 5 [5] => 4 [6] => 3 [7] => 1 
)
  •  Tags:  
  • Related