【问题标题】:How to simplify if ( ! in_array( $x, $a ) ) $a[] = $x; in php?如何简化 if ( ! in_array( $x, $a ) ) $a[] = $x;在 PHP 中?
【发布时间】:2016-02-23 08:32:12
【问题描述】:

我想写一些类似的东西

array_unique( $a[] = $x )

但它给了我一个通知:

array_unique() expects parameter 1 to be array, integer given.

有没有办法避免写

if ( ! in_array( $x, $a ) ) $a[] = $x;

?

【问题讨论】:

  • 你到底想达到什么目的?
  • array_unique 不会将元素添加到数组中,它会删除重复值。
  • 不,你不能真正简化这个。如果您只想在数组中不存在新条目时添加到数组中,在某个隐藏或明显的地方,您必须进行测试。
  • 我想您正在尝试在 Python 中模拟 set() 之类的东西:一个在您添加数据时自动删除重复数据的集合...?
  • 我不太明白这一点,但如果你也使用 value 作为 key,你可以更简化它。至少对于数字/字符串值:$a[$x] = $x; 不会有任何重复值。不过,您的代码中可能会使用更多的内存而不是更少的字符......

标签: php conditional-statements simplify


【解决方案1】:

我相信您正在尝试做的是一种数据结构,它会在您添加数据时自动删除重复数据(例如 Python 中的set())。 PHP 默认没有这样的东西。

两种方法:

  1. 使用item的唯一id作为key:

    $a[$x['id']] = $x;
    

    显然,这为您提供了自动重复数据删除功能。

  2. set() 之类的东西作为一个类实现:

    class Set implements ArrayAccess {
    
        protected $data = [];
    
        public function offsetSet($offset, $value) {
            if (is_null($offset)) {
                $this->data[] = $value;
            } else {
                $this->data[$offset] = $value;
            }
            $this->data = array_unique($this->data);
        }
    
        public function offsetExists($offset) {
            return isset($this->data[$offset]);
        }
    
        public function offsetUnset($offset) {
            unset($this->data[$offset]);
        }
    
        public function offsetGet($offset) {
            return isset($this->data[$offset]) ? $this->data[$offset] : null;
        }
    
    }
    
    $a = new Set()
    $a[] = 'foo';
    $a[] = 'foo'; // deduplicated
    

【讨论】:

  • 谁先用$a[$x] = $x;?你还是杰伦?
  • Rasmus Lerdorf 可能。 ;)
【解决方案2】:

我可以想到两种方法。

添加它们,然后删除重复项。

或者使用数组搜索(只是因为它不是你要求的 in_array)首先检查数组是否已经包含一个值。

<?php

$array = array('foo', 'bar', 'baz');
$array[] = 'foo';
$array[] = 'bat';
$array = array_unique($array); // Remove duplicates
var_dump($array);
$array = array_values($array); // Re-index array if you don't want to preserve keys
var_dump($array);

$array = array('foo', 'bar', 'baz');
if(false === array_search('foo', $array)) $array[] = 'foo';
if(false === array_search('bat', $array)) $array[] = 'bat';
var_dump($array);

输出:

array (size=4)
  0 => string 'foo' (length=3)
  1 => string 'bar' (length=3)
  2 => string 'baz' (length=3)
  4 => string 'bat' (length=3)

array (size=4)
  0 => string 'foo' (length=3)
  1 => string 'bar' (length=3)
  2 => string 'baz' (length=3)
  3 => string 'bat' (length=3)

array (size=4)
  0 => string 'foo' (length=3)
  1 => string 'bar' (length=3)
  2 => string 'baz' (length=3)
  3 => string 'bat' (length=3)

【讨论】:

    【解决方案3】:

    你总是可以创建你的函数:)

    function my_array_unique($a, $val) {
        $a[] = $val;
        return array_unique($a);
    }
    

    【讨论】:

    • $x 从何而来?
    猜你喜欢
    • 2016-05-24
    • 2012-09-10
    • 2021-11-20
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多