Convert an Object to an Array in PHP

Convert an Object to an Array in PHP

In some cases, an object may be returned, instead of an array.  This example shows you how to convert an object into an array.

Method 1:

$json = file_get_contents('url_returning_json');
$obj = json_decode($json); //becomes an object

$array_from_object = get_object_vars($obj); //convert to array

 

Method 2: JSON 

$array = json_decode(json_encode($nested_object), true);      

Share this Post