theHack
theHack

Reputation: 2014

Collect all unique key paths of a multidimensional array

[
    'a' => [
        'a' => ['a' => null, 'b' => null],
        'b' => ['a' => null, 'b' => null]
    ],
    'b' => [
        'a' => ['a' => null, 'b' => null],
        'b' => ['a' => null, 'b' => null]
    ]
]

How can I get the below string from the above array?

aaa,aab,aba,abb,baa,bab,bba,bbb

Upvotes: -2

Views: 1289

Answers (4)

mickmackusa
mickmackusa

Reputation: 47894

I wanted to add a modern, functional-style recursive function based on @AndrewMoore's script.

None of the logic has changed. Iterate over all keys of a level. If the value is an array, traverse into the array. Prepend the carried prefix to each encountered key.

array_map() version: (Demo)

function getKeyPaths(array $array, string $prefix = ''): string
{
    return implode(
        ',',
        array_map(
            fn($k, $v) => is_array($v) ? getKeyPaths($v, "$prefix$k") : "$prefix$k",
            array_keys($array),
            $array
        )
    );
}

var_export(getKeyPaths($array));

array_reduce() version: (Demo)

function getKeyPaths(array $array, string $prefix = ''): string
{
    return implode(
        ',',
        array_reduce(
            array_keys($array),
            fn($carry, $k) => $carry
                + [
                   $k => is_array($array[$k])
                       ? getKeyPaths($array[$k], "$prefix$k")
                       : "$prefix$k"
                ],
            []
        )
    );
}
var_export(getKeyPaths($array));

Upvotes: 0

Siddharth Shukla
Siddharth Shukla

Reputation: 1131

Multidimensional array in PHP

$Student = array(array("Adam",10,10),

                array("Ricky",10,11),

                array("Bret",15,14),

                array("Ram",14,17)
          );

for($i=0;$i<=3;$i++){

    for($j=0;$j<=2;$j++){
        print_r($Student[$i][$j]);

        echo "<br>";    
    }
}

Upvotes: -1

Gaurav
Gaurav

Reputation: 28755

 $str = array();
foreach($array as $key1 => $value1)
{
   foreach($value1 as $key2 => $value2)
   {
     foreach($value2 as $key3 => $value3)
        $str[]= $key1.$key2.$key3;
   }
}

echo implode(',', $str);

Upvotes: 2

Andrew Moore
Andrew Moore

Reputation: 95334

You could simply write a recursive function to automatically concatenate the keys together.

function getKeysString($array, $prefix = '') {  
  $keys = array();

  foreach($array as $key => $value) {
    $str = $prefix.$key;

    if(is_array($value)) {
      $str = getKeysString($value, $str);
    }

    $keys[] = $str;
  }

  return implode(',', $keys);
}

So, given the array:

$arr = array (
  'a' => array (
    'a' => array (
      'a' => null,
      'b' => null
    ),
    'b' => array (
      'a' => null,
      'b' => null
    )
  ),
  'b' => array (
    'a' => array (
      'a' => null,
      'b' => null
    ),
    'b' => array (
      'a' => null,
      'b' => null
    )
  )
);

The following would give you the result you want:

$result = getKeysString($arr);

Upvotes: 7

Related Questions