i would like to flatten or on some similar way achieve the desired structure of array.
at the moment my array looks like this:
$list = array(
'ind' => array(
'messagetype' => 'Alert',
'visibility' => 'Public',
'info' => array(
0 => array(
'urgency' => 'Urgent',
'params' => array(
0 => array(
'Name' => 'display',
'value' => '3; top',
),
1 => array(
'Name' => 'level',
'value' => '1; blue',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GSSD154',
),
),
),
),
1 => array(
'urgency' => 'Minor',
'params' => array(
0 => array(
'Name' => 'display',
'value' => '1; left',
),
1 => array(
'Name' => 'level',
'value' => '1; red',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GBECS23',
),
),
),
),
),
),
);
I would like to get it just flattened on that way or with some other method to get this kind of output which does basicaly the same as flatten functions, so basicaly just moves childs up on the same level and renames them with "." dots.
$list = array(
'ind' => array(
'messagetype' => 'Alert',
'visibility' => 'Public',
'info' => array(
0 => array(
'urgency' => 'Urgent',
'params.0.Name' => 'display',
'params.0.value' => '3; top',
'params.1.Name' => 'level',
'params.1.value' => '1; blue',
'area.ard' => 'Bob',
'area.code.0.Name' => 'Badge',
'area.code.0.Value' => 'GSSD154',
),
1 => array(
'urgency' => 'Minor',
'params.0.Name' => 'display',
'params.0.value' => '1; left',
'params.1.Name' => 'level',
'params.1.value' => '1; red',
'area.ard' => 'Bob',
'area.code.0.Name' => 'Badge',
'area.code.0.Value' => 'GBECS23',
),
),
),
);
?>
I need to preserve info => 0 keys, the keys that are containing params => Name needs to contain some identificator so in this case the easiest is to preserve key which is in almost all cases [0] [1], but in some [0] [1] [2]. Where the key is contained it does not really matters for me.
I have tried all the stuff I could find on internet, but I just could not find good effect to fit this.
with following code:
function makeNonNestedRecursive(array &$out, $key, array $in) {
foreach ($in as $k => $v) {
if (is_array($v)) {
makeNonNestedRecursive($out, $key . $k . '.', $v);
} else {
$out[$key . $k] = $v;
}
}
}
function makeNonNested(array $in) {
$out = array();
makeNonNestedRecursive($out, '', $in);
return $out;
}
//$fooCompressed = makeNonNested($list);
echo "<pre>";
print_r($list);
the Output is
Array
(
[ind.messagetype] => Alert
[ind.visibility] => Public
[ind.info.0.urgency] => Urgent
[ind.info.0.params.0.Name] => display
[ind.info.0.params.0.value] => 3; top
[ind.info.0.params.1.Name] => level
[ind.info.0.params.1.value] => 1; blue
[ind.info.0.area.ard] => Bob
[ind.info.0.area.code.0.Name] => Badge
[ind.info.0.area.code.0.value] => GSSD154
[ind.info.1.urgency] => Minor
[ind.info.1.params.0.Name] => display
[ind.info.1.params.0.value] => 1; left
[ind.info.1.params.1.Name] => level
[ind.info.1.params.1.value] => 1; red
[ind.info.1.area.ard] => Bob
[ind.info.1.area.code.0.Name] => Badge
[ind.info.1.area.code.0.value] => GBECS23
)
I just need it to not proccess throught info[num] and preserve that value for further operations.
Thanks for all the help in forward.