Home > Software engineering >  How to get hyrarchy from key elements and create new elements based on that - PHP
How to get hyrarchy from key elements and create new elements based on that - PHP

Time:02-19

I have a very big array, I will try to explain the issue in small examples:

Input:

Array ( 
[alert:accountDisabled:heading] => XYZ 
[alert:accountDisabled:message] => XYZ
[alert:accountExpired:heading] => XYZ 
[alert:accountExpired:message] => XYZ
[alert:errorResponse:heading] => XYZ 
[button:back] => XYZ 

)

What I need to get is:

array() { 
    ["alert"]=> array(7) { 
        ["accountDisabled"]=> array(2) { 
            ["heading"]=> string(3) "XYZ" 
            ["message"]=> string(3) "XYZ" } 
       ["accountExpired"]=> array(2) { 
           ["heading"]=> string(3) "XYZ" 
           ["message"]=> string(3) "XYZ" } 
      ["clientError"]=> array(2) { 
          ["heading"]=> string(3) "XYZ" 
          ["message"]=> string(3) "XYZ" } 
      ["errorResponse"]=> array(1) { 
          ["heading"]=> string(3) "XYZ" } 
 }
 ["button"]=> array(1) { 
     ["back"]=> string(3) "XYZ"
 }

As I said this is a very small example, but the point is to get hierarchy from keys from array number one, hierarchy is divided by this character in key :

I checked for those questions that look similar to this one but they are not helpful lat all

How to access and manipulate multi-dimensional array by key names / path?

Using a string path to set nested array data

SO please read carefully the description of my issue. I tried to use it for each loop, and I succeed to divide elements from the key, for one element, but I'm not sure where I need to store those hierarchy values for the next elements, any ideas?

CodePudding user response:

$input = [
    'alert:accountDisabled:heading' => 'XYZ_1',
    'alert:accountDisabled:message' => 'XYZ_2',
    'alert:accountExpired:heading'  => 'XYZ_3',
    'alert:accountExpired:message'  => 'XYZ_4',
    'alert:errorResponse:heading'   => 'XYZ_5',
    'button:back'                   => 'XYZ_6'
];

$results = [];

foreach ($input as $key => $value) {
  $arr = explode(':', $key);
  $result = $value;
  for ($i = count($arr) - 1; $i >= 0; $i--) {
    $result = [ $arr[$i] => $result ];
  }
  $results[] = $result;
}

$result = array_merge_recursive(...$results);

print_r($result);

Output:

Array
(
    [alert] => Array
        (
            [accountDisabled] => Array
                (
                    [heading] => XYZ_1
                    [message] => XYZ_2
                )

            [accountExpired] => Array
                (
                    [heading] => XYZ_3
                    [message] => XYZ_4
                )

            [errorResponse] => Array
                (
                    [heading] => XYZ_5
                )

        )

    [button] => Array
        (
            [back] => XYZ_6
        )

)

CodePudding user response:

Based on Lukas.j answer, you can use this function:

function parsePath($array, $separator = ':'){
    $result = [];
    foreach($array as $key => $value){
       if(strpos($key, $separator) !== FALSE){
           $keys = explode($separator, $key);
           $inner_result = $value;
           foreach (array_reverse($keys) as $valueAsKey) $inner_result = [$valueAsKey => $inner_result];
           $result[] = $inner_result;
       }
    }
    return array_merge_recursive(...$result);
}
  • Related