I'm attempting to make an array of users with ids and data. Normally the data is received from a database, but here it's hard-coded. I'm trying to make the users array return an associative array with an id and an array of data. For some reason, the arrays accessed with 'data' have no elements instead of each containing 5 and 6.
$users = array();
$users[] = array( "id" => 1, "data" => array() );
$users[] = array( "id" => 2, "data" => array() );
foreach( $users as $user_row ) {
$user_row['data'] [] = 5;
$user_row['data'] [] = 6;
}
How can I get the inner arrays to add in the data?
The foreach
loop is creating copies of your sub-arrays, and so the changes made inside the loop do not persist. Add an ampersand to make $user_row
reference the originals instead of making copies:
foreach( $users as &$user_row )