0

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.

Romanoffa
  • 71
  • 2
  • 9
  • 1
    [This](https://stackoverflow.com/questions/10424335/php-convert-multidimensional-array-to-2d-array-with-dot-notation-keys) should help. – Jeto Mar 29 '20 at 16:05

2 Answers2

0

I don't know if I understand your problem correctly. You can flatten the subarray $list['ind']['info']. I use this tableArray() class to show the result.

$data = tableArray::create($list['ind']['info'])
  ->flatten()
  ->fetchAll()
;

//test output
echo '<pre>';
var_export($data);

Result:

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',
  ),
) 
jspit
  • 7,276
  • 1
  • 9
  • 17
0

Give a man a fire and he's warm for the day. But set fire to him and he's warm for the rest of his life.

I am, on purpose, giving you a very basic level example ("a fire") so that you can modify it to remove duplications, add more controls so on. in order to improve your basic PHP skills ("warm for the day"). You can easily reduce it to half the size! There are helper classes etc. out there but try to do it yourself first.

$result = [];
$result['ind']['messagetype'] = $list['ind']['messagetype'];
$result['ind']['visibility'] = $list['ind']['visibility'];

$lines = [];
foreach ($list['ind']['info'] as $k => $v) {
    if (is_array($v)) {
        $lines[$k]['urgency'] = $v['urgency'];
        if (is_array($v['params'])) {
            foreach ($v['params'] as $kp => $vp) {
                if (is_array($vp)) {
                    foreach ($vp as $kpi => $vpi) {
                        $lines[$k]['params.'.$kp.'.'.$kpi] = $vpi;
                    }
                }
            }
        }

        if (is_array($v['area'])) {
            foreach ($v['area'] as $kp => $vp) {
                if (is_array($vp)) {
                    foreach ($vp as $kpi => $vpi) {
                        if (is_array($vpi)) {
                            foreach ($vpi as $kpid => $vpid) {
                                $lines[$k]['area.'.$kp.'.'.$kpi.'.'.$kpid] = $vpid;
                            }
                        }
                    }
                } else {
                    $lines[$k]['area.'.$kp] = $vp;
                }
            }
        }
    }
}
$result['ind']['info'] = $lines;

print_r($result);
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
                        )
                )
        )
)
BentCoder
  • 12,257
  • 22
  • 93
  • 165