在 PHP 中更新多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10179961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:35:12  来源:igfitidea点击:

Updating a Multidimensional Array in PHP

phparrays

提问by Talon

I have an array that looks like this

我有一个看起来像这样的数组

$array =
    Array
    (
    [0] => Array
        (
            [Product] =>  Amazing Widget
            [Value] => 200
        )

    [1] => Array
        (
            [Product] => Super Amazing Widget
            [Value] => 400
        )

    [2] => Array
        (
            [Product] =>  Promising Widget 
            [Value] => 300
        )

    [3] => Array
        (
            [Product] => Superb Widget
            [Value] => 400
        )
    }

I want to update the array to change "Promising Widget" to 800 instead of 300.

我想更新数组以将“Promising Widget”更改为 800 而不是 300。

Note that the order of this array is arbitrary, meaning that I need to update the Value Based on the "Product" name value (not on it's number in the array).

请注意,此数组的顺序是任意的,这意味着我需要根据“产品”名称值(而不是数组中的编号)更新值。

I was trying to access it via the number in the array but realized that wouldn't work for that reason and I'm not sure how to change the value of one element of a multidimensional array based on another.

我试图通过数组中的数字访问它,但意识到由于这个原因这不起作用,我不确定如何根据另一个元素更改多维数组的一个元素的值。

Thanks for any help.

谢谢你的帮助。

回答by Rocket Hazmat

foreach($array as &$value){
    if($value['Product'] === 'Promising Widget'){
        $value['Value'] = 800;
        break; // Stop the loop after we've found the item
    }
}

So, you loop through the array, find value you want, then change it. The &$valueis so the array is passed by reference. Meaning we can directly edit the values in the array from the loop, without having to do $array[$key]['Value'].

所以,你遍历数组,找到你想要的值,然后改变它。的&$value是这样的阵列通过引用传递。这意味着我们可以直接从循环中编辑数组中的值,而无需执行$array[$key]['Value'].

回答by Nick

I think you'd have to loop through them, something like:

我认为您必须遍历它们,例如:

foreach ($array as $k => $v) {
  if ($v['Product']=='Promising Widget') {
    $array[$k]['Value']=800;
  }
}

回答by jmarceli

I think that most universal approach is to use array_walk_recursivefunction like that:

我认为最通用的方法是使用这样的array_walk_recursive函数:

array_walk_recursive($array, 'updateValue');

function updateValue(&$data, $key) {
  if($key == 'Promising Widget') {
    $data = 800;
  }
}

This way even if you will change your array later on this function still will be working fine.

这样,即使您稍后会在此函数上更改数组,它仍然可以正常工作。

回答by Abu Roma?ssae

This answer might be too late, but I faced a similar issues which I solved using this function

这个答案可能为时已晚,但我遇到了使用此功能解决的类似问题

function r_search_and_replace( &$arr ) {
    foreach ( $arr as $idx => $_ ) {
        if( is_array( $_ ) ) r_search_and_replace( $arr[$idx] );
        else {
            if( is_string( $_ ) ) $arr[$idx] = str_replace( "PATTERN", "REPLACEMENT", $_ );
        }
    }
}

回答by Theva

For those who expect for a most complicated array, use a recursive function to go through all the elements

对于那些期望最复杂数组的人,使用递归函数遍历所有元素

(assume this function is in a php class, then)

(假设这个函数在一个 php 类中,那么)

public function reGenerateArray(&$arr)
{
    array_walk($arr, function (&$v, $k ) {
        if($k === 'KEY_NAME') {
            $v['OTHER_KEY'] = $newValueToReplace;
        } elseif("array" == gettype($v)) {
            $this->reGenerateArray($v);
        }
    });
}

This function will reproduce the existing array

此函数将重现现有数组