PHP - Replace data within multidimensional array, specific key

前端 未结 2 1820
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-19 09:14

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 ke

相关标签:
2条回答
  • 2020-12-19 10:01
    foreach($array as &$value) {
        $value['ad_type'] = 'new value';
    }
    
    0 讨论(0)
  • 2020-12-19 10:06

    Best way to access the keys and values of an array is with foreach loop.

    Something like:

    $array= 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 ) );
    
    foreach ($array as $key=>$val) 
    {
        if ($key=="ad_type" && $val==1) 
        {
            $val="x";
        }
        elseif ($key=="ad_type" && $val==2) 
        {
            $val="y";
        }
    }
    

    For further reference http://php.net/manual/en/control-structures.foreach.php

    0 讨论(0)
提交回复
热议问题