Home >Backend Development >PHP Problem >php array removal object
In PHP programming, arrays and objects are one of the most commonly used data types. Sometimes we need to convert an array containing objects into an array containing only original data. In this case, we need to remove objects from the array. This article will introduce how to remove objects from an array in PHP.
array_map
function is a function in PHP that applies a function to each element in an array, and then Return a new array. For arrays containing objects, we can use the array_map
function to apply a function to convert each object to its original data type. For example:
function object_to_array($obj) { return (array) $obj; } $arr = array( (object)array("name"=>"Tom","age"=>18), (object)array("name"=>"Jerry","age"=>20), ); $new_arr = array_map("object_to_array",$arr); print_r($new_arr);
In the above code, we define a object_to_array
function, which will convert an object into an array. We then use the array_map
function to apply this function to the original array $arr
, returning a new array and storing it in the $new_arr
variable. Finally, we use the print_r
function to output the converted array.
The json_decode
function in PHP can convert JSON format data into a PHP array or object. The json_encode
function can convert PHP arrays or objects into JSON format data. Therefore, we can first use the json_encode
function to convert the array containing objects into JSON format data, and then use the json_decode
function to convert it into an array containing only primitive data types. For example:
$arr = array( (object)array("name"=>"Tom","age"=>18), (object)array("name"=>"Jerry","age"=>20), ); $json = json_encode($arr); $new_arr = json_decode($json,true); print_r($new_arr);
In the above code, we first use the json_encode
function to convert the array $arr
into JSON format data and store it in $json
Variable, then use the json_decode
function to convert it into an array and store it in the $new_arr
variable. Finally, we use the print_r
function to output the converted array.
The ArrayIterator
class in PHP can convert an array into an iterator, and then we can use the method getArrayCopy
Convert it to an array containing only primitive data types. For example:
$arr = array( (object)array("name"=>"Tom","age"=>18), (object)array("name"=>"Jerry","age"=>20), ); $iterator = new ArrayIterator($arr); $new_arr = $iterator->getArrayCopy(); print_r($new_arr);
In the above code, we create an instance of the ArrayIterator
class $iterator
and pass the array $arr
to it . We then use the getArrayCopy
method to convert it to an array of primitive data type and store it in the $new_arr
variable. Finally, we use the print_r
function to output the converted array.
To sum up, we can use the above three methods to remove objects from arrays in PHP. In specific applications, we can choose the appropriate method according to actual needs.
The above is the detailed content of php array removal object. For more information, please follow other related articles on the PHP Chinese website!