Monday, 9 January 2017

php - Error When Pushing Key/Value Pair Array with array_push

Answer


Answer





I'm trying to push a key/value pair to an array like so:



$holders_array = array();

foreach ($holders as $holder) {
array_push($holders_array, "date" => $holder['date'], "holders" => $holder['holders']);
}



But I am getting the error:




Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in




I see that you cannot push key-value pairs with array_push according to this link, however, I can't figure out how to get it right.




What do I need to do to push the key value pair to the array? Thanks!


Answer



you could simply do as:



$holders_array = array();

foreach ($holders as $holder) {
$holders_array[] = [
"date" => $holder['date'],
"holders" => $holder['holders']

];
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...