Usually in PHP, it takes a bit of effort to work with an array of objects.
But here today, I'm telling you a so-easy black technology to solve this problem.
<?php/** * Created by Phpstorm. * USER:ZRJ * date:17-10-20 * Time: PM 8:08 */declare (Strict_types=1);//Open strong Type mode class person{public $name; public $age; Public function __construct (string $name, int $age) { $this->name = $name; $this->age = $age; }} $jack = new Person (' Jack '); Echo Print_r ($jack, true); echo "<p>";//object to array $jack = Json_decode (Json_encode ($jack) , true); Echo Print_r ($jack, true);
Let's look at the results:
Person Object ( [name] + Jack [age] + =) Array ( [name] + Jack [age] + 18)
Thinking Analysis:
The object is first json_encode processed into a JSON string.
The converted JSON string is json_decode processed.
Json_decode (Json_encode ($obj), true);
Summarize:
Advantages: Simple and quick.
Disadvantage: Double-fold memory is consumed.
The correct get posture:
The object itself occupies memory hours and can be used directly.
When the object itself occupies a large memory (such as thousands of records to form a DataSet object), twice times of memory may result in exceeding the memory limit of PHP and an exception occurs.