Is it possible to create array like that:
Array
(
[Name] => John
[Last Name] => Doe
[Age] => 19
)
from nested array like that:
[1] => Array
(
[Name] => John
)
[2] => Array
(
[Last name] => Doe
)
[3] => Array
(
[Age] => 19
)
Nested array is created when adding values in array while making a loop
foreach ($users as $user) {
$users[] = array($user['title'] => $user['value']);
}
More simplified @NigelRen's solution is using array_column
:
$output = array_column($users, 'value', 'title');
You should change the way the array is built rather than process it further. You are currently adding new arrays each time...
$output = [];
foreach ($users as $user) {
$output[$user['title']] = $user['value'];
}
You could use array_walk_recursive
it will also work if you have nested array
array_walk_recursive($a, function($v, $k) use (&$new){ $new[$k]=$v;});
DEMO:- https://3v4l.org/UGQou
You can use array_reduce
$result = array_reduce(array(
array("Name"=>"John"),
array("Last Name" => "Doe"),
array("Age" => 19)
),function($a,$b){return array_merge($a,$b);},[]);
print_r($result);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.