Reputation: 7577
so for example i have something like
array:2 [▼
"old" => array:2 [▼
"uk" => "unk"
"en" => "eng"
"fr" => "fre"
]
"new" => array:2 [▼
"uk" => "united kingdom"
"en" => "english"
"fr" => "french"
]
]
atm i can only get one item according to a predefined key
foreach ($data as $status => $value) {
$str .= $value['uk'];
}
which give
unk united kingdom
instead i want to get all of them, so the result would be
unk united kingdom
eng english
fre french
which could be translated into an array or combined values
array:3 [▼
"uk" => [
"unik",
"united kingdom"
],
"en" => [
"eng",
"english"
],
"fr" => [
"fren",
"french"
]
]
so what's the best way to achive that ?
Upvotes: 1
Views: 44
Reputation: 3165
You just need to flatten the sub-arrays :
$output = array();
foreach ($data as $status => $codeArray) {
foreach ($codeArray as $code=>$value){
$output[$code][]=$value;
}
}
Upvotes: 1
Reputation: 8308
you may go with array_walk_recursive as follows:
$output = [];
array_walk_recursive($arr, function ($value, $key) use (&$output) {
$output[$key][] = $value;
});
live example: https://3v4l.org/aKYsG .
Upvotes: 2