1

I have a php array that stores other nested arrays. The data to be inserted into the array is sent as "a_b_c_x", "a_b_c_y", "a_b_c_z_p", "a_b_d", etc. the four strings mentioned above need to the stored in an array as:

[
  a = [
    b = [
      c = [
        x = [],
        y = [],
        z = [
          p = []
        ]
      ],
      d = []
    ] 
  ]
]

The array can have unknown number of nestings. I need to parse the string to search for existing keys and add new ones. I tried something like:

foreach($productConfigurationAdd as $toAdd) {
    $addArray = explode('_', $toAdd);

    $addTo = &$savedConfigurations;

    foreach($addArray as $addElem) {
        if(array_search($addElem, $addTo) === false) {
            $addTo[$addElem] = [];
            $addTo = &$addTo[$addElem];
        }
        else {
            $addTo = &$addTo[$addElem];
        }
    }
}

and it is only saving the first children of each block. Please let me know what is going wrong here.

Edit:

In the above code, $savedConfigurations is the array that is obtained from the database and if the add string contains new configurations, it is stored in $savedConfigurations as mentioned in the question.

2
  • You have to create a recursive function. Because you ve got X depht. Commented Jul 27, 2018 at 9:25
  • Sorry, there was a mistake in the code. I used "array_search", while i was supposed to use "array_key_exists". Commented Jul 27, 2018 at 10:26

3 Answers 3

4

The solution for this is quite simple. No need for a recursive function.

Simply create a reference of the array key in succession and assign the value in the end.

function put(&$var, $path, $value) {
    foreach(explode('_', $path) as $p) $var =& $var[$p];
    $var = $value;
}

Usage example:

$data = [];

put($data, 'a_b_c_x', 'foo');
put($data, 'a_b_c_y', 'bar');
put($data, 'a_b_c_z_p', [123]);
put($data, 'a_b_d', 456.789);

print_r($data);

Output:

Array
(
    [a] => Array
        (
            [b] => Array
                (
                    [c] => Array
                        (
                            [x] => foo
                            [y] => bar
                            [z] => Array
                                (
                                    [p] => Array
                                        (
                                            [0] => 123
                                        )

                                )

                        )

                    [d] => 456.789
                )

        )

)

See it in action at https://3v4l.org/Q7SIq

Sign up to request clarification or add additional context in comments.

Comments

1

Cool question. I would write such recursive function:

function array_put(&$array, $path, $value, $ix = 0) {
 $path_ex = explode('_', $path);
 if ($ix == count($path_ex) - 1) {
  $array[$path_ex[$ix]] = $value;
  return;
 }
 else {
  array_put($array[$path_ex[$ix]], $path, $value, $ix+1);
 }
}

$data = [];
array_put($data, 'a_b_c_x', 'test');
array_put($data, 'a_b_c_y', []);
array_put($data, 'a_b_c_z_p', 123);
array_put($data, 'a_b_d', null);

var_dump($data);

Try it online!

Comments

0

Sorry, there was a stupid mistake in the code. I used "array_search", while i was supposed to use "array_key_exists".

1 Comment

Yes it does. Thank you.

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.