print_r($members) coming like this following result
Array ( [myname] => Array ( [userid] => 52 [age] => 46 )
Array ( [hisname] => Array ( [userid] => 22 [age] => 47 )
Array ( [yourname] => Array ( [userid] => 47 [age] => 85 )
array_push() push not working in the foreach loop
foreach($members as $key => $item){
// print "<br>" . $key ."<br>";
array_push($members, '$key');
}
The result expecting like this following code with the array_push()
Array ( [myname] => Array ( [userid] => 52 [age] => 46 [0] => myname)
Array ( [hisname] => Array ( [userid] => 22 [age] => 47 [0] => hisname)
Array ( [yourname] => Array ( [userid] => 47 [age] => 85 [0] => yourname)
result
Warning: array_push() expects parameter 1 to be array, null given in C:\xampp\htdocs\index.php on line 126
Warning: array_push() expects parameter 1 to be array, null given in C:\xampp\htdocs\index.php on line 126
Warning: array_push() expects parameter 1 to be array, null given in C:\xampp\htdocs\index.php on line 126
$arrin your foreach?$arras an (empty) array. Place this before the loop:$arr=[];. No idea why that happens in all iterations though...'$key'pushes the literal string "$key" onto the array, not the content of that variable.$members, the same array which you are iterating. That seems like a bad idea.