1

I have a nested array who is 4 levels deep.

I would like to replace the keys by numerical ones but not the meta level, this array should keep the alphabetical keys.

The array will have a lot of values and there will be more accounts like Marco Mueller, this is just a short example. It is important that it will keep the same nested structure, after its replaced by numerical keys.

$array = array (
    'Marco Mueller' =>
        array (
            'meta' =>
                array (
                    'accountName' => 'Marco Mueller',
                    'accountId' => '1',
                    'sum' => '3,659.06',
                ),
            'Conrad, Ute' =>
                array (
                    'meta' =>
                        array (
                            'customerName' => 'Conrad, Ute',
                            'customerId' => '8391',
                            'sum' => '457.59',
                        ),
                    'Fidor Bank' =>
                        array (
                            'meta' =>
                                array (
                                    'bankName' => 'Fidor Bank',
                                    'bankKey' => 'FID',
                                    'sum' => '457.59',
                                ),
                            'H1-2019' =>
                                array (
                                    'meta' =>
                                        array (
                                            'periodName' => 'H1-2019',
                                            'periodId' => '5',
                                            'sum' => '457.59',
                                        ),
                                ),
                        ),
                ),
        ),
);

The result should be

$array = array (
        array (
            'meta' =>
                array (
                    'accountName' => 'Marco Mueller',
                    'accountId' => '1',
                    'sum' => '3,659.06',
                ),
             'items' => array (
                    'meta' =>
                        array (
                            'customerName' => 'Conrad, Ute',
                            'customerId' => '8391',
                            'sum' => '457.59',
                        ),
                     'items' => 
                           array (
                               'meta' =>
                                   array (
                                    'bankName' => 'Fidor Bank',
                                    'bankKey' => 'FID',
                                    'sum' => '457.59',
                                   ),
                                'items' => array (
                                    'meta' =>
                                        array (
                                            'periodName' => 'H1-2019',
                                            'periodId' => '5',
                                            'sum' => '457.59',
                                        ),
                                ),
                            ),

                            array (
                                    'meta' =>
                                        array (
                                            'periodName' => 'H2-2019',
                                            'periodId' => '6',
                                            'sum' => '600',
                                        ),
                                ),
                           ),
                ),
        ),
);

1 Answer 1

1

I solved my problem with this function, if anyone has an idea how to make this more elegant and efficient, your welcome!

private function getValues(array $array)
    {
        $newArray = [];
        foreach ($array as $item) {
            $meta = $item['meta'];
            unset($item['meta']);

            $arrayValues = array_values($item);

            if (count($arrayValues) > 1 || count($arrayValues[0]) > 1) {
                $arrayValues = $this->getValues($arrayValues);
            }

            if (!$arrayValues) {
                $values = [
                    'meta' => $meta,
                ];
            } else {
                $values = [
                    'meta' => $meta,
                    'items' => $arrayValues,
                ];
            }

            $newArray[] = $values;


        }

        return $newArray;
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.