-1

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!

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
ShittyDeveloper
  • 117
  • 1
  • 8

2 Answers2

1

you could simply do as:

$holders_array = array();

foreach ($holders as $holder) {
    $holders_array[] = [
        "date" => $holder['date'],
        "holders" => $holder['holders']
    ];
}
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
0

You can write your logic like

foreach ($holders as $holder) {
    $date_array['date'] = $holder['date'];
    $holder_array['holders'] = $holder['holders'];
    array_merge($holders_array, $date_array,$holder_array);
}