Pergunta

I have this exath path saved somewhere:

Array
(
    [0] => library
    [1] => 1
    [2] => book
    [3] => 0
    [4] => title
    [5] => 1
)

I have some array and I want to change the value on this index:

$values[library][1][book][0][title][1] = "new value";

I have no idea, how to do this, because there can be any (unknown) number of dimensions. Any hints?

Foi útil?

Solução

It makes sense to create a function that does this, so:

function array_path_set(array & $array, array $path, $newValue) {
    $aux =& $array;
    foreach ($path as $key) {
        if (isset($aux[$key])) {
            $aux =& $aux[$key];
        } else {
            return false;
        }
    }
    $aux = $newValue;
    return true;
}
$values = array(
    'library' => array(
        1 => array(
            'book' => array(
                0 => array(
                    'title' => array(
                        1 => 'MAGIC VALUE!',
                    ),
                ),
            ),
        ),
    ),
);
$path = array('library', 1, 'book', 0, 'title', 1);
$newValue = 'ANOTHER MAGIC VALUE!';

var_dump($values);
var_dump(array_path_set($values, $path, $newValue));
var_dump($values);

Outras dicas

Try

foreach ($array as $val) {
   $indexes .= "[$val]";
}
${'output'.$indexes} = 'something';

Or

$indexes = '';
foreach ($array as $val) {
   $indexes .= "[$val]";
}
$output = 'values'.$indexes;
$$output = 'something';

Are you trying to provide a new value for the title of book 0 in library 1? If so, you will have to search for library "1", with book "0" and then change the value of the "title". So if all your values in the array have the same six entries, start with loc = 0 look at the values at loc+1 (library id), loc+3 (book id) and loc+5 .. change title) .. if not increment loc by 6 and continue searching.

[sounds like homework, so no code provided. Pardon me if I am wrong.]

Just in case you weren't aware of it, the key

$values["library"][1]["book"][0]["title"][1]

Is not the same as the array example in your post. Presuming the array is $values, it has five elements:

$values = Array
(
    [0] => "library"
    [1] => 1
    [2] => "book"
    [3] => 0
    [4] => "title"
    [5] => 1
)
$values[0] = "Library";
$values[1] = "1";
$values[2] = "book";
$values[3] = "0";
etc...

Is this array structure what you intended? If not, post back with a more complete structure so we can help.

Also, you need quotes around the strings - I have included for clarity

You could take a look at the following link for array_search. Some of the posters have included examples of a multidimensional array search and there are other examples. Do a google search on "multidimensional array search" and you will likely find a solution. If you need more direction, post back your details.

try this ->

$keys = array('0'=>'for','1'=>'test','2'=>'only');
$value='ok';

function addArrayPathWithValue($keys,$value,$array = array(),$current = 
array())
{
    $function = __FUNCTION__;

    if (count($current)==0)
    {
        $keys = array_reverse($keys);
        $current = $value;
    }

    if (count($keys)==0)
    {
       return $current;
    }

    $array[array_shift($keys)]=$current;

    return $function($keys,$value,NULL,$array);
}

$array = addArrayPathWithValue($keys,$value);
print_r($array);
//output: Array ( [for] => Array ( [test] => Array ( [only] => ok ) ) )
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top