Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

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) 
like image 520
Eldar Rakhimberdin Avatar asked Apr 28 '14 08:04

Eldar Rakhimberdin


People also ask

Can you have an array of objects in PHP?

We can use the array() function to create an array of objects in PHP. The function will take the object as the arguments and will create an array of those objects. We can create objects by creating a class and defining some properties of the class. The properties of the class will have some values.

How can we fetch data from database and store in array in PHP?

Data can be fetched from MySQL tables by executing SQL SELECT statement through PHP function mysql_query. You have several options to fetch data from MySQL. The most frequently used option is to use function mysql_fetch_array(). This function returns row as an associative array, a numeric array, or both.

Can you put variables in an array PHP?

Yes, you can store variables within arrays, though you'll need to remove the space between $result and the opening bracket.


2 Answers

PHP 5

array_column doesn't work with an array of objects. Use array_map instead:

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

PHP 7

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().

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

like image 97
Daniel W. Avatar answered Sep 20 '22 22:09

Daniel W.


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

PHP 7

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

PHP 5 >= 5.5.0

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

// 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_column and use array_map with an anonymous function

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

or a simple foreach

$result = array(); foreach($arr_o as $o) {     $result[] = $o->my_string; } 
like image 32
cgaldiolo Avatar answered Sep 21 '22 22:09

cgaldiolo