简体   繁体   中英

Updating a Multidimensional Array in PHP

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.

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.

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'] .

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

I think that most universal approach is to use array_walk_recursive function like that:

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.

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM