PHP。是否可以将 array_column 与对象数组一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23335845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:39:10  来源:igfitidea点击:

PHP. Is it possible to use array_column with an array of objects

php

提问by Eldar Rakhimberdin

Is it possible to pass in array_columnan array of objects?
I have implemented ArrayAccess interface, but it has no effect.
Should I implement another one?

是否可以传入array_column对象数组?
我已经实现了 ArrayAccess 接口,但是没有效果。
我应该实施另一个吗?

class Foo implements ArrayAccess {

    public $Id, $Title;

    public function offsetExists($offset)
    {
        return isset($this->{$offset});
    }    

    public function offsetGet($offset)
    {
        return $this->{$offset};
    }

    public function offsetSet($offset, $value)
    {
        $this->{$offset} = $value;
    }

    public function offsetUnset($offset)
    {
        unset($this->{$offset});
    }
}

$object = new \Foo();
$object->Id = 1;
$object->Title = 'Test';

$records = array(
    $object, 
    array(
        'Id' => 2,
        'Title' => 'John'
    )
);

var_dump(array_column($records, 'Title')); // array (size=1) 0 => string 'John' (length=4)

回答by Daniel W.

PHP 5

PHP 5

array_columndoesn't work with an array of objects. Use array_mapinstead:

array_column不适用于对象数组。使用array_map来代替:

$titles = array_map(function($e) {
    return is_object($e) ? $e->Title : $e['Title'];
}, $records);

PHP 7

PHP 7

array_column()

array_column()

The function now supports an array of objects as well as two-dimensional arrays. Only public properties are considered, and objects that make use of __get()for dynamic properties must also implement __isset().

该函数现在支持对象数组和二维数组。只考虑公共属性,使用__get()动态属性的对象也 必须实现__isset().

See https://github.com/php/php-src/blob/PHP-7.0.0/UPGRADING#L629- Thanks to Bellfor the hint!

请参阅https://github.com/php/php-src/blob/PHP-7.0.0/UPGRADING#L629- 感谢Bell的提示!

回答by cgaldiolo

Is it possible to pass in array_column an array of objects?

是否可以在 array_column 中传递一个对象数组?

PHP 7

PHP 7

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

是的,请参阅http://php.net/manual/en/function.array-column.php

PHP 5 >= 5.5.0

PHP 5 >= 5.5.0

In PHP 5 array_columndoes not work with an array of objects. You can try with:

在 PHP 5array_column中不适用于对象数组。您可以尝试:

// object 1
$a = new stdClass();
$a->my_string = 'ciao';
$a->my_number = 10;

// object 2
$b = new stdClass();
$b->my_string = 'ciao b';
$b->my_number = 100;

// array of objects
$arr_o = array($a,$b);

// using array_column with an array of objects
$result = array_column(array_map(function($o){return (array)$o;},$arr_o),'my_string');

PS: for clarity I prefer to not use array_columnand use array_map with an anonymous function

PS:为了清楚起见,我更喜欢不使用array_column和使用带有匿名函数的array_map

$result = array_map(function($o){ return $o->my_string; }, $arr_o);

or a simple foreach

或者一个简单的 foreach

$result = array();
foreach($arr_o as $o) {
    $result[] = $o->my_string;
}

回答by Tofandel

Here is a function that will work on both php7 and php5

这是一个适用于 php7 和 php5 的函数

function array_column_portable($array, $key) {
    return array_map(function($e) use ($key) {
        return is_object($e) ? $e->$key : $e[$key];
    }, $array);
}

You can then use it as you use array_columnin php7

然后您可以像在 php7 中使用array_column一样使用它

回答by Igor Okto

Possible solution is prepare that array of objects:

可能的解决方案是准备该对象数组:

$objectsList = [];

foreach ($objs as $obj) {        
    $objectsList[] = (array)$obj;
}

$propList = array_column($objectsList, 'prop');

回答by yckart

While it's not possible to use array_columnon child-objects, you can cast them to arrays. This is of course not the way to go, but it's a way.

虽然无法array_column在子对象上使用,但您可以将它们转换为数组。这当然不是要走的路,但这是一条路。

Using array_map/get_object_vars(doesn't not work in your case, due the containing array)

使用array_map/get_object_vars(在您的情况下不起作用,因为包含数组)

array_column(array_map('get_object_vars', $thingy), 'property');

Using json_decode/json_encode

使用 json_decode/json_encode

array_column(json_decode(json_encode($thingy), true), 'property');

https://eval.in/597950

https://eval.in/597950

Note: Using json brings not the same result as using a true recursive function. You'll lose protected and private properties of the object. But in some situations its fine.

注意:使用 json 与使用真正的递归函数带来的结果不同。您将丢失对象的受保护和私有属性。但在某些情况下它很好。

function object_to_array($object) {
  if (is_object($object)) $object = get_object_vars($object);
  return is_array($object) ? array_map(__FUNCTION__, $object) : $object;
}

回答by Nikunj Dhimar

For future Visitor.

对于未来的访客。

$arrayColumnData = array_map(function($e) {
    return is_object($e) ? $e->your_column : $e['your_column'];
}, $yourArray);

Thanks.

谢谢。