Checking if array value exists in a PHP multidimensional array

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

I have the following multidimensional array:

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


        
3条回答
  •  我在风中等你
    2021-01-15 04:03

    Even though the question is answered, I wanted to post my answer. Might come handy to future viewers. You can create new array from this array with filter then from there you can check if value exist on that array or not. You can follow below code. Sample

    $arr = array(
            0 =>array(
                    "id"=> 1,
                    "name"=> "Bangladesh",
                    "action"=> "27"
                ),
             1 =>array(
                    "id"=> 2,
                    "name"=> "Entertainment",
                    "action"=> "34"
                     )
            );
    
         $new = array();
         foreach($arr as $value){
            $new[$value['id']] = $value; 
         }
    
    
        if(array_key_exists(1, $new)){
            echo $new[1]['id'];
        }
        else {
          echo "aaa";
        }
        //print_r($new);
    

提交回复
热议问题