I know this is old but you could try the following piece of code:
$array = json_decode(json_encode($object), true);
where $object is the response of the API.
Answer from kendepelchin on Stack OverflowI know this is old but you could try the following piece of code:
$array = json_decode(json_encode($object), true);
where $object is the response of the API.
You can use recursive function like below:
function object_to_array($obj, &$arr)
{
if (!is_object($obj) && !is_array($obj))
{
$arr = $obj;
return $arr;
}
foreach (
key => $value)
{
if (!empty($value))
{
key] = array();
objToArray($value,
key]);
}
else {
key] = $value;}
}
return $arr;
}
A quick way to do this is:
$obj = json_decode(json_encode($array));
Explanation
json_encode($array) will convert the entire multi-dimensional array to a JSON string. (php.net/json_encode)
json_decode($string) will convert the JSON string to a stdClass object. If you pass in TRUE as a second argument to json_decode, you'll get an associative array back. (php.net/json_decode)
I don't think the performance here vs recursively going through the array and converting everything is very noticeable, although I'd like to see some benchmarks of this. It works, and it's not going to go away.
The best way would be to manage your data structure as an object from the start if you have the ability:
$a = (object) array( ... ); $a->prop = $value; //and so on
But the quickest way would be the approach supplied by @CharlieS, using json_decode(json_encode($a)).
You could also run the array through a recursive function to accomplish the same. I have not benchmarked this against the json approach but:
function convert_array_to_obj_recursive($a) {
if (is_array($a) ) {
foreach(
k => $v) {
if (is_integer($k)) {
// only need this if you want to keep the array indexes separate
// from the object notation: eg.
a['index'][$k] = convert_array_to_obj_recursive($v);
}
else {
k] = convert_array_to_obj_recursive($v);
}
}
return (object) $a;
}
// else maintain the type of $a
return $a;
}
Hope that helps.
EDIT: json_encode + json_decode will create an object as desired. But, if the array was numerical or mixed indexes (eg. array('a', 'b', 'foo'=>'bar') ), you will not be able to reference the numerical indexes with object notation (eg. o[1]). the above function places all the numerical indexes into the 'index' property, which is itself a numerical array. so, you would then be able to do
$o->index[1]. This keeps the distinction of a converted array from a created object and leaves the option to merge objects that may have numerical properties.
Your object's properties are private. If you var_dump() an object as you did it prints also private properties.
Have a look at this article http://php.net/manual/en/language.oop5.iterations.php. You can iterate object as you do but it iterates only public properties.
First of all, we can't see the scope. All your code is private (object properties and the method). Show the scope and we can help you better.
The problem seems to be here:
if (!empty($value))
{
$arr[$key] = array();
objToArray($value, $arr[$key]);
}
You are passing a second argument that doesn't defined in the method
private function objToArray($obj){...}
So you are passing a value to objToArray(), that value is a string I guess, and when it reach this line:
$arr = array();
if(!is_object($obj) && !is_array($obj)){
$arr = $obj;
die("invalid data");
}
Then it will display "invalid data", and empty your array.
$new_array = array_map(function($el) { return $el['key']; }, $array);
<?php
$arr = array(array(141,151,161,140),2,3,array(101,202,array(303,404),407));
function array_oned($arrays){
static $temp_array = array();
foreach($arrays as $key){
if(is_array($key)){
array_oned($key);
}else {
$temp_array [] = $key;
}
}
return $temp_array;
}
echo print_r(array_oned($arr));
?>
Did you mean something like this ?