Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Replace data within multidimensional array, specific key

I'm relearning PHP, so sorry for might be a basic question. I can't find an answer.

I have a multidimensional array, I need to replace the value for a specific key (all instances of) with another value.

Array ( 
    [13] => Array ( 
                [ad_id] => 13 
                [ad_name] => Qhxxst 
                [ad_link] => www.qxxst.co.uk 
                [ad_type] => 1 
            ) 
    [15] => Array ( 
                [ad_id] => 15 
                [ad_name] => Pxxly 
                [ad_link] => http://pixxly.net 
                [ad_type] => 1 
            ) 
    [16] => Array ( 
                [ad_id] => 16 
                [ad_name] => cxxm 
                [ad_link] => http://www.cxxm.co.uk 
                [ad_type] => 1 
            ) 
)

I wish to replace all instances of ad_type with another value. i.e. Where ad_type = 1, replace with x Where ad_type = 2, replace with y

I've been using str_replace and json_decode without success. They either replace all instances of '1' or nothing at all. I need to target ad_type keys only.

like image 706
Damo Avatar asked Apr 24 '11 13:04

Damo


1 Answers

foreach($array as &$value) {
    $value['ad_type'] = 'new value';
}
like image 109
alex Avatar answered Sep 22 '22 08:09

alex