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.
Therefore, it is not recommended to use when encountering a large amount of objects.
This article is from the "My PHP path" blog, so be sure to keep this source http://phpme.blog.51cto.com/663593/1974734
Black Technology for PHP object to array