Talon
Talon

Reputation: 4995

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.

Upvotes: 16

Views: 41372

Answers (5)

Theva
Theva

Reputation: 948

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

Upvotes: 0

jmarceli
jmarceli

Reputation: 20162

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.

Upvotes: 8

Abu Romaïssae
Abu Romaïssae

Reputation: 3901

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

Upvotes: 2

gen_Eric
gen_Eric

Reputation: 227270

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

Upvotes: 30

Nick
Nick

Reputation: 6346

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

Upvotes: 13

Related Questions