Updating a Multidimensional Array in PHP

Posted on

Updating a Multidimensional Array in PHP – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and arrays . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about 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.

Solution :

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

Leave a Reply

Your email address will not be published. Required fields are marked *