在PHP中更新多维数组

时间:2021-11-03 21:26:51

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.

我想更新数组,将“有前途的小部件”改为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).

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

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.

感谢任何帮助。

4 个解决方案

#1


17  

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 &$value is 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'].

因此,循环遍历数组,找到所需的值,然后更改它。$值是这样的,数组是通过引用传递的。这意味着我们可以直接从循环中编辑数组中的值,而不需要做$array[$key]['Value']。

#2


5  

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;
  }
}

#3


4  

I think that most universal approach is to use array_walk_recursive function 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.

这样,即使您稍后更改该函数的数组,仍然可以正常工作。

#4


1  

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", $_ );
        }
    }
}

#1


17  

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 &$value is 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'].

因此,循环遍历数组,找到所需的值,然后更改它。$值是这样的,数组是通过引用传递的。这意味着我们可以直接从循环中编辑数组中的值,而不需要做$array[$key]['Value']。

#2


5  

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;
  }
}

#3


4  

I think that most universal approach is to use array_walk_recursive function 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.

这样,即使您稍后更改该函数的数组,仍然可以正常工作。

#4


1  

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", $_ );
        }
    }
}