【问题标题】:Check if a value exists in a php array like this?检查一个值是否存在于这样的 php 数组中?
【发布时间】:2014-07-31 12:08:57
【问题描述】:

给定一个像下面这样的数组,我如何在不使用循环等的情况下检查来自查询字符串的值是否存在于该数组中?

我厌倦了使用 in_array,但即使该值不存在,它似乎也会返回 true。

我需要检查 alias 部分中是否存在该值。

Codepad code of the same thing I posted below.

<?php
$categories = array (
    'CAT1' => array('alias' => 'mediterranean-cuisine', 'name' => 'Mediterranean Cuisine'),
    'CAT2' => array('alias' => 'asian-cuisine', 'name' => 'Asian Cuisine'),
    'CAT3' => array('alias' => 'greek-cuisine', 'name' => 'Green Cuisine')
);

$category = 'asian-cuisine';

if(in_array($category,$categories)) {
    echo 'A category with that name was not found! '.$category;
} else {
    echo 'A category was found '.$category;
}

echo '<br>';


$category = 'xxx-cuisine';

if(in_array($category,$categories)) {
    echo 'A category with that name was not found! '.$category;
} else {
    echo 'A category was found '.$category;
}
?>

【问题讨论】:

  • without using loops etc. 不管你是否需要循环:为什么这个“要求”?
  • 您为什么不想使用循环来检查数组中的某些内容?循环是编程中最基本/最基本的控制结构之一......另外,in_array() 在内部使用循环,如果这很重要
  • 也许他只是想递归。如果您有没有多个嵌套循环的多维数组,则不能使用简单的foreach
  • @Machavity 如何不能在多维数组上使用foreachforeach ($categories as $category =&gt; $data) { if ($data['alias'] === $_GET['whatever']) { ... } }
  • 这听起来像是代码高尔夫,让我们使用array_filter

标签: php arrays


【解决方案1】:

您需要遍历数组并检查是否存在这样的值

$categories = array (
    'CAT1' => array('alias' => 'mediterranean-cuisine', 'name' => 'Mediterranean Cuisine'),
    'CAT2' => array('alias' => 'asian-cuisine', 'name' => 'Asian Cuisine'),
    'CAT3' => array('alias' => 'greek-cuisine', 'name' => 'Green Cuisine')
);

$category = 'asian-cuisine';

$found = false;
foreach($categories as $cat) {
    if($cat['alias'] == $category) {
        echo 'A category was found '.$category;
        $found = true;
        break;
    }
}

if(!$found)  {
    echo 'A category with that name was not found! '.$category;
}

【讨论】:

    【解决方案2】:

    在 PHP >= 5.5.0 中,您可以使用 array_column() 来获取别名数组。

    $aliases = array_column($categories, 'alias');
    

    然后你可以使用in_array():

    $category = 'asian-cuisine';
    
    if(in_array($category, $aliases)) {
        echo 'A category was found '.$category;
    } else {
        echo 'A category with that name was not found! '.$category;
    }
    
    echo '<br>';
    
    
    $category = 'xxx-cuisine';
    
    if(in_array($category, $aliases)) {
        echo 'A category was found '.$category;
    } else {
        echo 'A category with that name was not found! '.$category;
    }
    

    【讨论】:

      【解决方案3】:

      使用“Foreach”导航到一个数组:

      <?php
      $categories = array (
          'CAT1' => array('alias' => 'mediterranean-cuisine', 'name' => 'Mediterranean Cuisine'),
          'CAT2' => array('alias' => 'asian-cuisine', 'name' => 'Asian Cuisine'),
          'CAT3' => array('alias' => 'greek-cuisine', 'name' => 'Green Cuisine')
      );
      
      $category = 'asian-cuisine';
      
      foreach($categories as $c => $k)
      {
          if(in_array($category,$k))
              echo $category." founded in array ".$c."<br/>";
      }
      

      在 PHP 手册中查看有关 Foreach 的更多信息 => http://php.net/manual/en/control-structures.foreach.php

      编辑:不够快 =)

      【讨论】:

        【解决方案4】:

        由于您使用的是多维数组并且您要查找的值未设置为键,因此您需要在此处使用循环。但是,如果您真的想避免循环,您可以采取一些方法。

        第一个可能也是最有效的方法是将您要查找的别名存储在另一个数组中,但作为该数组的键:

        $aliases = array(
            'mediterranean-cuisine' => 1,
            'asian-cuisine' => 1,
            'greek-cuisine' => 1
        );
        

        然后您可以使用array_key_exists()isset() 来确定您要查找的值是否存在:

        if (isset($aliases[$_GET['alias']])) {
            echo 'Found!';
        }
        

        因为数组是 PHP 中的哈希映射,所以它不会在内部使用实际的循环,并且应该比任何基于循环的方法更有效。

        对此的替代方法可以使用数组回调/遍历函数,例如array_filter()

        function alias_check($category) {
            return ($category['alias'] === $_GET['alias']);
        }
        
        $matches = array_filter($categories, 'alias_check');
        if (count($matches) > 0) {
            echo 'Found!';
        }
        

        在内部,这个 确实 遍历数组中的每个元素,因此从技术上讲它不会避免循环;但是,您不必自己写一个很好的。

        不过,最直接和最简洁的方法是使用简单的循环:

        foreach ($categories as $category) {
            if ($category['alias'] === $_GET['alias']) {
                echo 'Found';
                break;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-07-11
          • 2020-06-09
          • 1970-01-01
          • 2016-06-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多