Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Set the value of a multidimensional associative array element using a path defined in a separate array

Tags:

arrays

loops

php

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;
  }
}
like image 976
DanH Avatar asked May 25 '11 10:05

DanH


People also ask

What is a multidimensional array in PHP?

A multidimensional array in PHP an be treated as an array of arrays so that each element within the array is an array itself. Inner elements of a multi dimensional array may be associative or indexed. Although arrays can be nested upto any levels, two dimensional array with more than one dimensional arrays inside outermost is of realistic use

What are the different types of 2D arrays in PHP?

There are three different types of 2D Arrays in PHP which are the following: 1 Numeric Array 2 Associative Array 3 Multidimensional Array

How to create a multidimensional associative array in Python?

Multidimensional associative array is often used to store data in group relation. Creation: We can create a multidimensional associative array by mapping an array containing a set of key and value pairs to the parent key. Explanation: In above program, parent index are Python and PHP.

How to access arrays in multidimensional arrays?

Arrays or sub-arrays in multidimensional arrays can be accessed using multiple dimensions. Dimensions: Dimensions of multidimensional array indicates the number of indices needed to select an element. For a two dimensional array two indices to select an element. Two dimensional array: It is the simplest form of a multidimensional array.


1 Answers

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;
    }
like image 181
mario Avatar answered Oct 19 '22 05:10

mario