更改多维数组php的元素

时间:2013-04-02 23:33:43

标签: php multidimensional-array

我有这个多维数组,我需要更改type字段,当我找到parc值时,在这种情况下100在此数组中使用php ...但总是我找到了最后一个数组替换总数组。有人可以帮帮我吗?非常感谢...对不起我的英语不好。

Array
(
    [1] => Array
        (
            [designacao] => 
            [imagem] => 
            [produto] => KITCHENAID Robot de cozinha Artisan 5KSM150PSETG cor-de-laranja
            [preco_t] => 710
            [t_parc] => Parcela 1
            [parc] => 100
            [symbol] => €
            [zona] => COZINHA
            [id] => 0
            [type] => checkbox
        )

    [2] => Array
        (
            [designacao] => 
            [imagem] => 
            [produto] => KITCHENAID Robot de cozinha Artisan 5KSM150PSETG cor-de-laranja
            [preco_t] => 710
            [t_parc] => Parcela 2
            [parc] => 100
            [symbol] => €
            [zona] => COZINHA
            [id] => 0
            [type] => checkbox
        )
)

2 个答案:

答案 0 :(得分:0)

foreach ($array as $key => $value) {
    if ($key == 'parc' && $value == 100) {
        $array['type'] = 'something new';
    }
}

类似的东西?

答案 1 :(得分:0)

试试这个:

$source = array(); // your array

$keyName = 'parc';
$keyValue = 100;
$keyToChange = 'type';

array_walk($source, function (&$element) use ($keyName, $keyValue, $keyToChange) {
    if (
        array_key_exists($keyName, $element)
        && $element[$keyName] === $keyValue
    ) {
        $element[$keyToChange] = '[do_something]';
    }
});