ntan
ntan

Reputation: 2205

Convert a slash-delimited string into an associative multidimensional array

I want to create a multidimensional array based on a string.

the string has value $string="1/2/3"

and I want to assign $array[1][2][3]=something

actually the index of the array are described inside the $string

The $string has not same depth.

For example may be $string="1/2 OR $string="1/2/3/4/5 OR $string="1/2/3/5/7/8/9/9/6 so the number of keys in the multidimensional array is not standard.

Upvotes: 0

Views: 201

Answers (4)

Rafael
Rafael

Reputation: 18522

Try with

<?php
$ind = '1/2/3/4/5/6/7/8/9/10/11/12';

$ind = '[' . join('][', explode('/',$ind)) . ']';
$fn = create_function('$var, $val','global ${$var}; ${$var}'. $ind. '= $val;');
$array = array();
$fn("array", "something");
echo '<pre>'.print_r($array, true).'</pre>';
?>

Much simplier version

<?php
$ind = '1/2/3/4/5/6/7/8/9/10/11/12';
$ind = '[' . join('][', explode('/',$ind)) . ']';
$array = array();
$val = "something";
eval('$array'.$ind.'=$val;');
echo '<pre>'.print_r($array, true).'</pre>';
?>

Some people could kill me for the eval function, but it works in this case perfectly :-P

Upvotes: 1

Max Shawabkeh
Max Shawabkeh

Reputation: 38603

Here's a recursive solution that would work for any number of indices:

function set_by_indices(&$arr, $indices, $val) {
  if (count($indices) == 1) {
    $arr[$indices[0]] = $val;
  } else {
    set_by_indices($arr[$indices[0]], array_slice($indices, 1), $val);
  }
}

$arr = array(array(array(1 => 1, 2 => 4),
                   array(1 => -1, 2 => -2)),
             array(array(1 => 11, 2 => 14),
                   array(1 => -11, 2 => -12)));
$str = '0/0/1';

$indices = explode('/', $str);

print_r($arr);
set_by_indices($arr, $indices, 99);
echo '<br/>';
print_r($arr);

Output:

Array ( [0] => Array ( [0] => Array ( [1] => 1 [2] => 4 ) [1] => Array ( [1] => -1 [2] => -2 ) ) [1] => Array ( [0] => Array ( [1] => 11 [2] => 14 ) [1] => Array ( [1] => -11 [2] => -12 ) ) ) 
Array ( [0] => Array ( [0] => Array ( [1] => 99 [2] => 4 ) [1] => Array ( [1] => -1 [2] => -2 ) ) [1] => Array ( [0] => Array ( [1] => 11 [2] => 14 ) [1] => Array ( [1] => -11 [2] => -12 ) ) )

Upvotes: 0

Matteo Riva
Matteo Riva

Reputation: 25060

Assuming your string format:

list($lev1, $lev2, $lev3) = explode('/', $string);
$array[$lev1][$lev2][$lev3] = $something;

After your edit -- you can use a recursive function like this:

$string = '1/2/3/4/5/6/7/8';
$value  = 'something';

print_r(build_array($string, $value));

function build_array($string, $value, $array = array()) {
    list($key, $rest) = explode('/', $string, 2);
    if ( $key ) {
        $array = array(
            $key => build_array($rest, $value, $array[$key])
        );
        return $array;
    } else {
        return $value;
    }
}

Upvotes: 0

Matt Huggins
Matt Huggins

Reputation: 83269

$string = '1/2/3';
list($x, $y, $z) = explode('/', $string);

$array[$x][$y][$z] = 'something';

Upvotes: 2

Related Questions