Php array of objects to array of values

The solution depends on the PHP version you are using. At least there are 2 solutions:

First (Newer PHP versions)

As @JosepAlsina said before the best and also shortest solution is to use array_column as following:

$catIds = array_column($objects, 'id');

Notice: For iterating an array containing \stdClasses as used in the question it is only possible with PHP versions >= 7.0. But when using an array containing arrays you can do the same since PHP >= 5.5.

Second (Older PHP versions)

@Greg said in older PHP versions it is possible to do following:

$catIds = array_map(create_function('$o', 'return $o->id;'), $objects);

But beware: In newer PHP versions >= 5.3.0 it is better to use Closures, like followed:

$catIds = array_map(function($o) { return $o->id; }, $objects);

The difference

First solution creates a new function and puts it into your RAM. The garbage collector does not delete the already created and already called function instance out of memory for some reason. And that regardless of the fact, that the created function instance can never be called again, because we have no pointer for it. And the next time when this code is called, the same function will be created again. This behavior slowly fills your memory...

Both examples with memory output to compare them:

BAD

while (true)
{
    $objects = array_map(create_function('$o', 'return $o->id;'), $objects);

    echo memory_get_usage() . "\n";

    sleep(1);
}

// the output
4235616
4236600
4237560
4238520
...

GOOD

while (true)
{
    $objects = array_map(function($o) { return $o->id; }, $objects);

    echo memory_get_usage() . "\n";

    sleep(1);
}

// the output
4235136
4235168
4235168
4235168
...

This may also be discussed here

Memory leak?! Is Garbage Collector doing right when using 'create_function' within 'array_map'?

Php array of objects to array of values

Introduction to PHP object to array

The following article provides an outline for PHP object to array. As we all know, object is known as a class instance which has memory allocated. In the case of an array, it is a data structure containing one or more values of a similar type in a single name. On the other hand, an associative array is not like a normal PHP array. An associative array is an array that consists of a string index which stores item values linked with key values other than in order of the linear index.

Methods of a PHP object to array

Now, let us see different ways in which we can convert PHP object to array.

Method 1 – With the help of the json_decode and json_encode method.

In this method, the function json_decode takes JSON encoded string and changes it into a PHP variable, whereas the json_encode function returns a string which is encoded in a json format for a particular value.

Syntax:

$arr = json_decode(json_encode ( $obj ) , true);

Method 2 – With the help of type casting.

Typecasting is a technique in which one data type variable into the another data type. It is considered as an explicit data type conversion. It can translate a PHP object to an array with the help of typecasting rules in PHP.

Syntax:

$arr = (array) $obj;

How to Convert object to array in PHP?

As we all know, there are several formats of data like strings, objects, arrays etc. In the case of PHP also, there are data formats like this. In order to get the needed output, a php object obj result is needed in a format of an associative array.

Now, let us see how to translate a php object.

Code:

class hospital
{
// elements
. . . .
function __construct( $dis1, $dis2, $dis3)
{
// Use this pointer
. . . .
}
// create class object
. . .
// convert object to array
. . . .
?>

This is the skeleton for converting an object into an array.

Now let us see how to perform this.

  • In order to encode the string, use “object = json_encode($array);”

When the object is var_dump, all items will be displayed.

  • For decoding into an object, a json string which is available will be used to convert and string formatting is done to an object. It will be done using $obj = json_decode(json_encode($arr));
  • When the object is var_dump, all items will be displayed after converting into an array.

Here, one important point to consider is json_decode that translates a json string to an object, except you offer another option that is boolean which can be true or false. Even if the second parameter is considered as true, an array will be obtained.

Also, when json encode operation and decode operation are used, arrays are converted to objects that take up many resources if the array is large. In that case, the better method to type cast an array to an object that uses the object cast.

Consider $obj = (object) $arr; syntax. Here also, object will be converted into arrays.

Based on the requirements, you can choose the method you want for the conversion of an array into an object in PHP.

Examples of a PHP object to array

Different examples are mentioned below:

Example #1

PHP program to convert an object to an array using the typecasting method.

Code:

class hospital
{
var $el1;
var $el2;
var $el3;
function __construct( $dis1, $dis2, $dis3)
{
$this->item1 = $dis1;
$this->item2 = $dis2;
$this->item3 = $dis3;
}
}
// Creation of object for the class
$dis = new hospital("D", "S", "C") ;
echo "Items before conversion : " ;
var_dump($dis);
// convert object to array
$arr = (array)$dis;
echo "Items after conversion : ";
var_dump($arr);
?>

Output:

Php array of objects to array of values

In this program, a class hospital is created, and inside that, three elements such as el1, el2, and el3. Then, a __construct() function is declared, which gets executed during the time object is created. Once this is done, the constructor takes parameters that are later offered during the object creation using the keyword “new”. From the program, it can be seen that objects get printed in the first case of expression var_dump(). But in the second case of expression, an object is casted into an array using the typecasting procedure.

Example #2

PHP program to convert an object to an array using json encode and json decode.

Code:

class hospital
{
var $el1;
var $el2;
function __construct( $dis1, $dis2 )
{
$this->item1 = $dis1;
$this->item2 = $dis2;
}
}
// Creating object
$dis = new hospital(500, "C");
echo "Items before conversion : " ;
var_dump($dis);
// convert object to array
$arr = json_decode(json_encode($dis), true);
echo "Items after conversion : ";
var_dump($arr);
?>

Output:

Php array of objects to array of values

In this program also, a class hospital is created, and inside that, two elements such as el1 and el2, are created. Then, a __construct() function is declared, which gets executed during the time object is created. Once this is done, the constructor takes parameters that are later offered during the object creation using the keyword “new”. From the program, it can be seen that objects get printed in the first case of expression var_dump(). But in the second case of expression, an object is casted into an array using the typecasting procedure. Here, the first method in the method sections is used for converting an object into an array.

Conclusion

An associative array is an array that consists of a string index which stores item values linked with key values other than in order of the linear index. This article saw how PHP object to array is working, methods to achieve it, and different examples.

This is a guide to PHP object to array. Here we discuss the introduction, methods, how to convert object to array in PHP? and examples respectively. You may also have a look at the following articles to learn more –

  1. PHP delete file
  2. PHP array_pop()
  3. PHP implode
  4. PHP Include and Require