Reputation: 7656
I've got this array
array:2 [
0 => array:2 [
"monday_open" => "10:00:00"
"monday_close" => "20:00:00"
]
1 => array:2 [
"tuesday_open" => "00:00:00"
"tuesday_close" => "00:00:00"
]
]
How can i combine them become:
array:4 [
"monday_open" => "10:00:00"
"monday_close" => "20:00:00"
"tuesday_open" => "00:00:00"
"tuesday_close" => "00:00:00"
]
I've tried using array_walk_recrusive but it doesn't return me with key name:
array_walk_recursive($array, function ($v) use (&$arrayFlat) {
$arrayFlat[] = $v;
});
I've tried this one too but got the same result as array_walk_recrusive:
iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), 0)
Result:
array:4 [
0 => "10:00:00"
1 => "20:00:00"
2 => "00:00:00"
3 => "00:00:00"
]
Is there any other way to keep the key value?
Upvotes: 0
Views: 52
Reputation: 971
You can add key param to the closure of the array_walk_recursive
like this
array_walk_recursive($array, function ($v, $k) use (&$arrayFlat) {
$arrayFlat[$k] = $v;
});
Upvotes: 1
Reputation: 475
Try this
$source_array = array(array("monday_open"=>"10:00:00", "monday_close" => "20:00:00"), array("tuesday_open" => "00:00:00", "tuesday_close" => "00:00:00"));
$my_array = array();
foreach($source_array as $source){
foreach($source as $key=> $val){
$my_array[$key] = $val;
}
}
print_r($my_array);
Upvotes: 1