Reputation: 41
I have the following array, coming from a form, with multilingual data like this:
Array
(
[en_name] => ...........
[en_description] => ...........
[gr_name] => ...........
[gr_description] => ...........
)
How can this array be converted into a two dimensional like:
Array
(
[en] => Array
(
[name] => ...........
[description] => ...........
)
[gr] => Array
(
[name] => ...........
[description] => ...........
)
)
Upvotes: 3
Views: 2958
Reputation: 5768
$array3d = array();
foreach($arr as $key => $value) {
$keyArr = explode("_", $key);
$array3d[$keyArr[0]][$keyArr[1]] = $value;
}
That should work provided that each key only has 1 underscore.
Upvotes: 1
Reputation: 7362
Try the following
$output = array();
foreach($arr as $val){
$prefix = str_replace("_","",substr($val,0,3));
$ending = substr($val,3,strlen($val));
if(!is_array($output[$prefix]))
$output[$prefix] = array();
array_push($output[$prefix],$ending);
}
Upvotes: 1
Reputation: 785631
Use this code:
$finalArr = array();
foreach($arr as $key => $val) {
$tok = explode('_', $key);
$finalArr[$tok[0]][$tok[1]] = $val;
}
Upvotes: 4