Seems like a pretty basic question, but how can I simplify an array such as:
Array
(
[0] => Array
(
[blue_dog_1] => 2
)
[1] => Array
(
[red_dog_1] => 4
)
[2] => Array
(
[red_dog_2] => 6
)
)
To be like:
Array
(
[blue_dog_1] => 2
[red_dog_1] => 4
[red_dog_2] => 6
)
Thanks in advance.
Try this way to make it single dimension from multi dimension using array_merge
$singleD = array_reduce($multiD, 'array_merge', array());
OR
$singleD = call_user_func_array('array_merge', $multiD);
Try this,
foreach($array as $sub_val)
{
foreach($sub_val as $key=>$val)
{
$new_array[$key] = $val;
}
}
print_r($new_array);
To accomplish this you can simply use array union operator .
$oldData = array(
0 => array('blue_dog_1'=>2),
1 => array('red_dog_1'=>4),
2 => array('red_dog_2'=>6)
);
$newData = array();
foreach ($oldData as $arrayData) {
$newData += $arrayData;
}
print_r($newData);
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.