Checking if array value exists in a PHP multidimensional array

后端 未结 3 363
天命终不由人
天命终不由人 2021-01-15 03:09

I have the following multidimensional array:

Array ( [0] => Array 
         ( [id] => 1 
           [name] => Jonah 
           [points] => 27 )
         


        
3条回答
  •  深忆病人
    2021-01-15 03:52

    you can first store the array with index equal to the id. for example :

     $arr =Array ( [0] => Array 
         ( [id] => 1 
           [name] => Jonah 
           [points] => 27 )
        [1] => Array 
         ( [id] => 2 
           [name] => Mark 
           [points] => 34 )
      );
     $new = array();
     foreach($arr as $value){
        $new[$value['id']] = $value; 
     }
    
    //So now you can check the array $new for if the key exists already 
    if(array_key_exists(1, $new)){
        $new[1]['points'] = 32;
    }
    

提交回复
热议问题