【问题标题】:Checking if array value exists in a PHP multidimensional array检查 PHP 多维数组中是否存在数组值
【发布时间】:2012-02-11 12:35:31
【问题描述】:

我有以下多维数组:

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

我目前正在使用foreach 循环从数组中提取值:

foreach ($result as $key => $sub)
{
    ...
}

但我想知道如何查看数组中的值是否已经存在。

例如,如果我想向数组添加另一个集合,但 id 是 1(所以这个人是 Jonah)并且他们的分数是 5,我可以将 5 添加到 @ 中已经创建的数组值987654325@而不是创建一个新的数组值?

所以在循环完成后,数组将如下所示:

Array ( [0] => Array 
         ( [id] => 1 
           [name] => Jonah 
           [points] => 32 )
        [1] => Array 
         ( [id] => 2 
           [name] => Mark 
           [points] => 34 )
      )

【问题讨论】:

    标签: php arrays multidimensional-array


    【解决方案1】:

    如何循环遍历您的数组,检查每个项目是否 id 是您要查找的项目?

    $found = false;
    foreach ($your_array as $key => $data) {
        if ($data['id'] == $the_id_youre_lloking_for) {
            // The item has been found => add the new points to the existing ones
            $data['points'] += $the_number_of_points;
            $found = true;
            break; // no need to loop anymore, as we have found the item => exit the loop
        }
    }
    
    if ($found === false) {
        // The id you were looking for has not been found, 
        // which means the corresponding item is not already present in your array
        // => Add a new item to the array
    }
    

    【讨论】:

    • 感谢您的建议 Pascal,只有一个问题 - 如果我不知道数组的 id,有没有办法遍历所有数组并检查它是否匹配(例如 [id] == 2[name] == Mark )?
    • 你只需要改变条件,反映你想要的;它会变成if ($data['id'] == $the_id_youre_lloking_for || $data['name'] == $the_name_youre_looking_for)
    【解决方案2】:

    您可以先存储索引等于 id 的数组。 例如:

     $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;
    }
    

    【讨论】:

      【解决方案3】:

      即使问题得到了解答,我还是想发布我的答案。对未来的观众可能会派上用场。您可以使用过滤器从此数组创建新数组,然后从那里您可以检查该数组上是否存在值。你可以按照下面的代码。 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-25
        • 2014-05-28
        • 2014-10-14
        • 2018-11-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多