8

Ok so I have an array that holds the following elements:

$array['a']['b'][0]['c'];
$array['a']['b'][1]['c'];
$array['a']['d'][0]['c']['c'];
$array['b']['c'];

Then in a separate array, I have defined the path to these values:

$structure[0] = array('a','b','#','c');
$structure[1] = array('a','d','#','c','c');
$structure[2] = array('b','c');

Finally, I have an array holding the values:

$values[0] = array('value0-0','value0-1');
$values[1] = array('value1-0');
$values[2] = array('value2-0');

I'm trying to find a simple function/loop that will be able to apply the values in $values to the array path of $array that is defined in $structure.

The end result would be:

$array['a']['b'][0]['c']='value0-0';
$array['a']['b'][1]['c']='value0-1';
$array['a']['d'][0]['c']['c']='value1-0';
$array['b']['c']='value2-0';

In the case of $values[0] or $values[1], it would be able to loop through each value and substitute the $structure element matching '#' with the iteration number for that particular $value.

Is this simply a case of knuckling down and writing a drawn out recursive function, or is there a smart construct or php function that could provide a more elegant solution?

SOLUTION:

Thanks to Mario, my eventual solution is:

foreach ($struct as $i=>$keys)
  foreach ($values[$i] as $val) {
    $r = & $array;

    foreach ($keys as $key) {

        if ($key == "#") { $key = $i; }

        $r = & $r[$key];    // move pointer to subarray
    }

    $r = $val;
  }
}
2
  • I think you are going to need to write some looping. Commented May 25, 2011 at 10:36
  • A question should never contain its own solution -- that is what "answers" are for. Commented Jan 24, 2024 at 0:37

1 Answer 1

6

You will have to work with references to traverse the target array:

function inject($array, $struct, $values) {

    foreach ($struct as $i=>$keys)
    foreach ($values[$i] as $val) {
        $r = & $array;

        foreach ($keys as $key) {

            if ($key == "#") { $key = count($r); }

            settype($r[$key], "array");
            $r = & $r[$key];    // move pointer to subarray
        }

        $r = $val;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much! I don't understand the need for settype() however, I haven't used it and it doesn't seem to be required. I've posted my final solution in my question.
@DanH: The settype avoids having to test for the existence of the subarrays. It might be redundant in your case, if the target array already contains the corrent (empty) structure. And now that I think of it & $r[$key] will actually create missing subarrays. So the settype typecast is not necessary.
Old question but still pertinent.. if you are trying to use the function above, the $array argument should be passed by reference, e.g. &$array, to allow the changes to be returned.

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.