Possible Duplicate:
How to modify an array's values by a foreach loop?
Why doesn't this work?
$user_list_array = array(
1 => array( "first_name" => "Jim" ),
2 => array( "first_name" => "Bob" )
)
foreach ($user_list_array as $item ) {
echo $item["first_name"];
$item["last_name"] = "test";
}
var_dump($user_list_array );
I can get the "first_name"s back, but can't add the "last_name";
You're modifying $item, which is a copy of the relevant entry fro $user_list_array
EITHER: (modify the original array)
foreach ($user_list_array as $key => $item ) {
echo $item["first_name"];
$user_list_array[$key]["last_name"] = "test";
}
OR: (by reference)
foreach ($user_list_array as &$item ) {
echo $item["first_name"];
$item["last_name"] = "test";
}
unset($item);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With