0

I have an array of this sort:

$array = [
        "Darren" => [
            "age" => "18",
            "work" => [
                "occupation" => "developer",
                "company" => "ABC Ltd"
            ]
        ],
        "John" => [
            "age" => "24",
            "work" => [
                "occupation" => "developer",
                "company" => "ABC Ltd",
                "url" => "www.example.com"
            ],
        ]
    ]

And would like to merge the keys with a dot in between, depending on the array's hierachy:

       "Darren.age"
       "Darren.work.occupation"
       "Darren.work.company"
       ...

The function that I made so far is

    public function buildExpressionKey($array, $parentKey = null){

        $expression = [];

        foreach($array as $key=>$value){

            if(is_array($value)){
               array_push($expression, $parentKey. implode(".", 
$this->buildExpressionKey($value, $key)));
           }else{
               array_push($expression, $key);
           }
       }

       return $expression;
   }

it is returning this value at the moment:

  [
    [0] => "age.Darrenoccupation.company"
    [1] => "age.Johnoccupation.company.url"
  ]

Was wondering if it is possible to make a function which automatically does merges the keys like that, thanks in advance :)

  • 5
    What have you tried so far? – Spoody Jan 19 '18 at 14:41
  • post first your try – user2342558 Jan 19 '18 at 14:42
  • *it can have more than 2 dimensions* - post the extended input array and show clearly the relations between nested keys – RomanPerekhrest Jan 19 '18 at 14:42
  • Are you trying to rename your keys? To perhaps flatten your multi-dimensional array for some reason? Give us an example of your expected output, i.e. when it has more than 2 dimensions. – Progrock Jan 19 '18 at 14:52
  • Possible duplicate of [I need an array\_keys\_recursive()](https://stackoverflow.com/questions/3872840/i-need-an-array-keys-recursive) – splash58 Jan 19 '18 at 15:06
  • @splash58 No, the other question just wants to echo all keys (with inner keys) in disregard of the structure – Philipp Maurer Jan 19 '18 at 15:07
  • @PhilippMaurer I don't see any diff - OP can print result array :) – splash58 Jan 19 '18 at 15:10
  • @splash58 The difference is, that the keys do not get concatenated based on the initial structure – Philipp Maurer Jan 19 '18 at 15:13
  • Possible duplicate of [Get array's key recursively and create underscore separated string](https://stackoverflow.com/questions/2749398/get-arrays-key-recursively-and-create-underscore-separated-string) – Philipp Maurer Jan 19 '18 at 15:13
  • This seems to be the most fitting duplicate – Philipp Maurer Jan 19 '18 at 15:14
  • basically I am trying to build a an expression for DynamoDb. Based on the array it will auto build an UpdateExpression. Url is here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.PHP.03.html#GettingStarted.PHP.03.03 – Darren Jones Jan 19 '18 at 15:24

2 Answers2

1

What you are currently asking for:

<?php

$people =
[
 'John' => 
    [
        'Occupation' => 'Developer',
        'Age' => 18
    ],
'Darren' =>
    [
        'Occupation' => 'Manager',
        'Age' => 40
    ]
];


foreach($people as $name => $value)
    foreach($value as $k => $v)
        $strings[] = $name . '.' . $k;

var_export($strings);

Output:

array (
  0 => 'John.Occupation',
  1 => 'John.Age',
  2 => 'Darren.Occupation',
  3 => 'Darren.Age',
)
Progrock
  • 7,373
  • 1
  • 19
  • 25
  • Quote form the question: **I would like to have a scalable approach which is done automatically via a custom function. So it can have more than 2 dimensions**. – Philipp Maurer Jan 19 '18 at 14:48
  • @PhilippMaurer wrapping the above in a function is trivial. But I suspect the OP actually wants to do more than generate some strings. Waiting for corrections/clarifications. – Progrock Jan 19 '18 at 14:50
0

Managed to resolve this issue :)

/**
 * @param $array
 * @return array
 */
public function buildExpressionKey($array){

    $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($array));
    $keys = array();
    foreach ($iterator as $key => $value) {
        // Build long key name based on parent keys
        for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) {
            $key = $iterator->getSubIterator($i)->key() . '.' . $key;
        }
        $keys[] = $key;
    }
    return $keys;
}

Found something similar on here: Get array's key recursively and create underscore separated string