I am attempting to push a list of strings into a PHP associative array. Ideally, my data structure looks like the following:
$foo = array("key" -> array())
So what I'm trying to do is loop and do something like the following:
while loop
array_push($foo["key"], some_val);
done loop
This isn't building the array though as I anticipated it to do. What's missing here?
This should work for you:
function array_push_assoc($array, $key, $value){
$array[$key] = $value;
return $array;
}
$array = array_push_assoc($array, 'key', 'value');
You also can simply do this:
$array["key"] = $value;
Simply do this :
While(COND){
$foo["key"] = $some_val;
}
var_dump($foo);
I wouldn't use array_push here if you are in a loop anyway.
foreach($newStuff as $key=>$value){ $foo[$key] = $value; }
If you're outside of a loop and you want to insert multiple items simultaneously then use array_push.