vutopi
vutopi

Reputation: 13

How map array of array to single assoc array?

I have:

array:2 [
  0 => array:1 [
    "FNAME" => "nullable|string"
  ]
  1 => array:1 [
    "LNAME" => "nullable|string"
  ]
]

And I try to get:

array:1 [
  "key" => "value"
]

I try map it, but has problem

Upvotes: 1

Views: 62

Answers (2)

u_mulder
u_mulder

Reputation: 54841

Two simple ways:

print_r(array_merge(...$arr));
// if `...` is not available (php < 5.6), then:
print_r(call_user_func_array('array_merge', $arr))

Upvotes: 1

HTMHell
HTMHell

Reputation: 6006

<?php
$array = [
    [
        "FNAME" => "nullable|string",
    ],
    [
        "LNAME" => "nullable|string",
    ]
];

$newArray = [];

foreach ($array as $item) {
    foreach ($item as $key => $value) {
        $newArray[$key] = $value;
    }
}

print_r($newArray);

Will output:

Array
(
    [FNAME] => nullable|string
    [LNAME] => nullable|string
)

Upvotes: 2

Related Questions