-1

I have an array like this:

0 => array:8 [▼
      "_id" => MongoId {#266 ▶}
      "name" => "New param"
      "default" => "900"
      "visibility" => "1"
      "type" => 1
      "only_numbers" => "1"
      "value" => "900"
      "available" => "1"
    ]

How to collapse this array into:

$arr["New param"] = "900";

Only as:

$arr = array(
   $a["name"] => $a["value"]
);
Blablacar
  • 583
  • 1
  • 5
  • 16

1 Answers1

3

It looks like your array is an element of another array (that's what 0 => at the top implies). So you need to index that containing array:

$arr[$a[0]['name']] = $a[0]['value'];

If you want to get all the elements of that other array, use a loop:

foreach ($a as $el) {
    $arr[$el['name']] = $el['value'];
}

or you could do:

$arr = array_combine(array_column($a, 'name'), array_column($a, 'value'));
Barmar
  • 741,623
  • 53
  • 500
  • 612