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 :)

11
  • 5
    What have you tried so far? Commented Jan 19, 2018 at 14:41
  • post first your try Commented Jan 19, 2018 at 14:42
  • it can have more than 2 dimensions - post the extended input array and show clearly the relations between nested keys Commented Jan 19, 2018 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. Commented Jan 19, 2018 at 14:52
  • Possible duplicate of I need an array_keys_recursive() Commented Jan 19, 2018 at 15:06

2 Answers 2

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',
)
Sign up to request clarification or add additional context in comments.

2 Comments

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.
@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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.