【问题标题】:Get unique value of one attribute from array of associative arrays从关联数组中获取一个属性的唯一值
【发布时间】:2015-10-24 02:10:12
【问题描述】:

我有一个这样的数组:

$a = array(
    0 => array('type' => 'bar', 'image' => 'a.jpg'),
    1 => array('type' => 'food', 'image' => 'b.jpg'),
    2 => array('type' => 'bar', 'image' => 'c.jpg'),
    3 => array('type' => 'default', 'image' => 'd.jpg'),
    4 => array('type' => 'food', 'image' => 'e.jpg'),
    5 => array('type' => 'food', 'image' => 'f.jpg'),
    6 => array('type' => 'food', 'image' => 'h.jpg')
)

如何找出唯一的类型值(食物、酒吧和默认值)?我可以在 foreach 循环中遍历数组,但有更好的方法吗?

【问题讨论】:

    标签: php arrays foreach unique unique-values


    【解决方案1】:

    在 PHP >= 5.3 中使用匿名函数:

    $unique_types = array_unique(array_map(function($elem){return $elem['type'];}, $a));
    

    对于以前的版本,您可以声明一个单独的函数:

    function get_type($elem)
    {
        return $elem['type'];
    }
    
    $unique_types = array_unique(array_map("get_type", $a));
    

    【讨论】:

      【解决方案2】:

      使用 PHP >= 5.5,你可以这样做:

      $ar = array_unique(array_column($a, 'type'));
      

      print_r($ar):

      Array ( 
          [0] => bar 
          [1] => food 
          [3] => default 
      )
      

      http://php.net/manual/en/function.array-column.php

      http://php.net/manual/en/function.array-unique.php

      【讨论】:

      • 不知道为什么这有 +5 票,这是一个聪明的解决方案,不是很直接。
      • @true 这怎么可能是更多straight forward?这看起来很简单。使用新版本语言中可用的更新功能应该受到赞赏,而不是感到困惑。
      • 对于 1. 必须查看 array_* 做了什么,他们返回什么,如果他们通过引用修改数组等等。这只是一个聪明的解决方案,这里不多说。请不要把它当作私人的。
      • 所以如果有人必须查查,为什么不查查呢?这就像在 21 世纪使用泥桶来冷却水,只是因为我不想阅读我昨天刚买的冰箱的电源规格 :) 另外,即使是 OP 提到他们想要的也不是在寻找老派的方法.
      • 没有什么私人的,这只是一个关于是否使用新版本语言中可用的新工具的小讨论,你有权像其他人一样发表你的意见,所以没有错:)
      【解决方案3】:

      不使用花哨的array_* 函数的老式方法。这种方式简单易懂。您不会想知道发生了什么,因为它非常简单。

      $a = array(
          0 => array('type' => 'bar', 'image' => 'a.jpg'),
          1 => array('type' => 'food', 'image' => 'b.jpg'),
          2 => array('type' => 'bar', 'image' => 'c.jpg'),
          3 => array('type' => 'default', 'image' => 'd.jpg'),
          4 => array('type' => 'food', 'image' => 'e.jpg'),
          5 => array('type' => 'food', 'image' => 'f.jpg'),
          6 => array('type' => 'food', 'image' => 'h.jpg')
      );
      
      $types = array();
      
      foreach($a as $key => $type) {
              if(! isset($types[$type['type']]))
                      $types[$type['type']] = $type['type'];
      }
      
      var_dump($types);
      

      【讨论】:

      • 实际上,由于提供的任何array_* 函数,最快的答案之一是输入数组复制。那里的语言结构更好。
      • 您提到语言结构很有趣,因为在阅读您的评论之前,我刚刚在其他答案之一中发表了有关语言结构的评论。
      【解决方案4】:

      试试这个

      $uniqueA = array_unique($a, "type");
      // then to output the array just type
      print_r($uniqueA);
      

      【讨论】:

        【解决方案5】:

        您也可以使用 array_reduce。

        这仅在属性的值是数组或对象时不起作用,因为它们不能设置为数组的键。

        function array_unique_attr($arr, $key) {
        
            return array_keys( array_reduce($arr, function($newArr, $event) {
        
                $newArr[$key] = true;
                return $newArr;
        
            }, []) );
        
        }
        
        $unique_types = array_unique_attr($a, 'type');
        

        【讨论】:

          猜你喜欢
          • 2020-08-21
          • 1970-01-01
          • 2013-02-04
          • 2011-03-31
          • 2023-01-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-28
          相关资源
          最近更新 更多