php – 如何在多维数组中检查数组键?

如何检查数组中是否存在数组键?

我需要检查数组中是否存在用户ID,我在下面找到了数组,

 Array
(

  [0] => Array
    (
        [user_id] => 1482309797
        [week] => 1
        [type] => 1
        [commission] => 4000
    )

[1] => Array
    (
        [user_id] => 1482309797
        [week] => 1
        [type] => 1
        [commission] => 0
    )

[2] => Array
    (
        [user_id] => 1482309797
        [week] => 1
        [type] => 1
        [commission] => 4000
    )

[3] => Array
    (
        [user_id] => 1482309797
        [week] => 1
        [type] => 1
        [commission] => 0
    )

[4] => Array
    (
        [user_id] => 1483096072
        [week] => 1
        [type] => 1
        [commission] => 4000
    )

[5] => Array
    (
        [user_id] => 1483333245
        [week] => 1
        [type] => 1
        [commission] => 2000
    )

)

我想检查用户ID是否存在,我试过下面的代码

        foreach ($com_array as $report) {

         $user_id=$report['user_id'];

        if(array_key_exists($user_id,$output_array)){
                echo "Eid found";
         }else{
                echo "id not found";
            }

        }

任何一个请帮忙.

最佳答案

  foreach ($com_array as $report) {
     if(isset($report['user_id'])){
         echo "Eid found";
     }else{
         echo "id not found";
     }
  }

尝试上面的代码,您将获得输出.

点赞