As far as I can tell, there is no prebuilt solution for this, so you can just roll your own:

function array_to_object($array) {
   $obj = new stdClass();

   foreach ($array as v) {
      if (strlen($k)) {
         if (is_array($v)) {
            $obj->{$k} = array_to_object($v); //RECURSION
         } else {
            $obj->{$k} = $v;
         }
      }
   }
   
   return $obj;
}
Answer from Jacob Relkin on Stack Overflow
🌐
GitHub
gist.github.com › victorbstan › 744478
recursively cast a PHP object to array · GitHub
ArrayObject::getArrayCopy() return shallow copy of array. ... @scmrus and @c01nd01r I actually understood this after trying it, but I forgot to remove the comment from here. Sorry. BTW, a recursive function in PHP is faster than the JSON way. ... Be care full if use encodings other than UTF-8. If you use that, json_encode will return false. See here https://www.php.net/manual/de/function.json-encode.php#115733 ... This will not be be applicable if the properties of the object you are trying to convert are declared as private
Discussions

Recursively loop through php array and convert object elements to array storing their class names - Stack Overflow
I've been hours trying to figure this, i know i'm missing something obvious. This is the problem: I have an array that some of its elements are objects, others are arrays and others are of other types. What i need is: Loop through the array and convert the object elements into array elements. Loop recursively ... More on stackoverflow.com
🌐 stackoverflow.com
July 9, 2016
Convert a PHP object to an associative array - Stack Overflow
I'm integrating an API to my website which works with data stored in objects while my code is written using arrays. I'd like a quick-and-dirty function to convert an object to an array. More on stackoverflow.com
🌐 stackoverflow.com
Properly convert JSON with arrays to CSV

Any solution is going to be fragile as you're going from a more expressive to a less expressive format. As long as you can guarantee that all your nested objects (milestone, assignee and author) will always have the same order of elements and will never themselves contain an object or array, you should be able to simply implode them:

Instead of:

foreach ($line['author'] as $value)
{
    array_push($line_array,$value);
}

Use:

array_push($line_array, implode('|', $line['author']));

(Side note: it's generally better to do $myarr[] = 'val'; than array_push($myarr, 'val'). More readable and slightly faster.)

More on reddit.com
🌐 r/PHPhelp
3
2
April 20, 2015
How To Find The Position Of A Key In A Multidimensional Array?
Off the top of my head, it seems you'll have to do the array_keys() and see if it's in there, although there's also array_key_exists(). if the key isn't there, then do foreach() to look at each of those elements'. If there's always exactly nested levels, it's not too bad. If it's a variable number of levels, you'll probably have to use a recursive function. You should also be able to use array_filter() with a custom function. There could be complications, like if more than one entry had that key, or if the key's value is an array. More on reddit.com
🌐 r/PHPhelp
3
1
July 1, 2023
🌐
Coderwall
coderwall.com › p › 8mmicq › php-convert-mixed-array-objects-recursively
PHP - Convert mixed array & objects recursively (Example)
June 11, 2021 - This snippet of code will recursively convert that data to a single type (array or object) without the nested foreach loops. Hope it helps someone! ... <?php abstract class Util { public static function object_to_array($d) { if (is_object($d)) $d = get_object_vars($d); return is_array($d) ?
🌐
PHP
php.net › manual › en › function.array-walk-recursive.php
PHP: array_walk_recursive - Manual
Applies the user-defined callback function to each element of the array. This function will recurse into deeper arrays. Passing an object as the array is allowed, and will be treated as if it had been cast to an array first.
🌐
PHPpot
phppot.com › php › php-object-to-array
PHP Object to Array Convert using JSON Decode - PHPpot
This program defines a custom function to convert a PHP object to array. It performs the conversion recursively on each level of the input object.
🌐
Lobaugh
ben.lobaugh.net › blog › php-recursively-convert-an-object-to-an-array
PHP: Recursively convert an object to an array | Ben Lobaugh
Simply pass it your object and it will munch away at it trying to convert it into a PHP array. Take a gander at the code for this below. function object_to_array($obj) { if(is_object($obj)) $obj = (array) $obj; if(is_array($obj)) { $new = array(); foreach($obj as $key => $val) { $new[$key] ...
🌐
GitHub
gist.github.com › mlconnor › 2989980
php function to walk an array/object recursively · GitHub
php function to walk an array/object recursively. GitHub Gist: instantly share code, notes, and snippets.
Find elsewhere
🌐
Fellow Tuts
fellowtuts.com › home › php › 3 ways – convert a php object to an array and vice-versa
3 Ways - Convert a PHP Object to an Array and Vice-Versa
December 21, 2018 - PHP’s JSON functions can also do the object to the array or vice-versa conversion smartly. Additionally, it works perfectly with nested objects to convert them as an associative array. This is the best solution if you want a full depth recursive conversion.
🌐
Eric
eric.blog › 2018 › 08 › 05 › recursively-cast-to-array-in-php
Recursively cast to array in PHP – Eric Binnion
August 5, 2018 - * @return mixed */ function recursive_cast_to_array( $input ) { if ( is_scalar( $input ) ) { return $input; } return array_map( 'recursive_cast_to_array', (array) $input ); } ... Simple but very useful. Thank you for the tip. ... You’re welcome! Thanks for leaving a comment. ... I gave it a try, it is really great. Concise code has a beauty of its own. Thanks for sharing Eric. Loved it. ... Thanks for the code Eric! I believe this could be improved by updating the return statement like the following: return array_map( __FUNCTION__, (array) $input ); Just to play nicely with namespaces.
Top answer
1 of 16
1825

Just typecast it

$array = (array) $yourObject;

From Arrays:

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.

Example: Simple Object

$object = new StdClass;
$object->foo = 1;
$object->bar = 2;

var_dump( (array) $object );

Output:

array(2) {
  'foo' => int(1)
  'bar' => int(2)
}

Example: Complex Object

class Foo
{
    private $foo;
    protected $bar;
    public $baz;

    public function __construct()
    {
        $this->foo = 1;
        $this->bar = 2;
        $this->baz = new StdClass;
    }
}

var_dump( (array) new Foo );

Output (with \0s edited in for clarity):

array(3) {
  '\0Foo\0foo' => int(1)
  '\0*\0bar' => int(2)
  'baz' => class stdClass#2 (0) {}
}

Output with var_export instead of var_dump:

array (
  '' . "\0" . 'Foo' . "\0" . 'foo' => 1,
  '' . "\0" . '*' . "\0" . 'bar' => 2,
  'baz' =>
  stdClass::__set_state(array(
  )),
)

To know more about php built-in class stdclass. You can read this documentation

Typecasting this way will not do deep casting of the object graph and you need to apply the null bytes (as explained in the manual quote) to access any non-public attributes. So this works best when casting StdClass objects or objects with only public properties. For quick and dirty (what you asked for) it's fine.

Also see this in-depth blog post:

  • Fast PHP Object to Array conversion
2 of 16
540

You can quickly convert deeply nested objects to associative arrays by relying on the behavior of the JSON encode/decode functions:

$array = json_decode(json_encode($nested_object), true);
🌐
Barattalo
barattalo.it › coding › find-values-recursively-inside-complex-json-objects-in-php
Find values recursively inside complex json objects in PHP | Giulio Pons
This function can be used to quickly and easily search complex, nested php structures for specific values, allowing you to quickly locate the information you need. function find_recursive($obj, $key) { $found = array(); if ( is_object($obj) ) { foreach ($obj as $property => $value) if($property === $key) $found[] = $value; elseif (is_array($value) || is_object($value)) $found = array_merge( $found, find_recursive($value, $key) ); } elseif ( is_array($obj) ) { foreach ($obj as $keyar => $value) if($keyar === $key) $found[] = $value; elseif (is_array($value) || is_object($value)) $found = array_merge( $found, find_recursive($value, $key) ); } return $found; }
🌐
Rich Jenkins' Blog
richjenks.com › converting objects and arrays
Converting Objects and Arrays | Rich Jenkins' Blog
March 15, 2018 - As of PHP 5.4 you can use the short array syntax, which replaces array() with [] You could iterate through each array item / object property and cast it, then recursively check if it has children which need casting too, but PHP’s JSON functions offer an easier way.
🌐
If Not True Then False
if-not-true-then-false.com › 2009 › php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object
PHP stdClass to Array and Array to stdClass &#8211; stdClass Object :: If Not True Then False
December 30, 2009 - <?php function objectToArray($d) { if (is_object($d)) { // Gets the properties of the given object // with get_object_vars function $d = get_object_vars($d); } if (is_array($d)) { /* * Return array converted to object * Using __FUNCTION__ (Magic constant) * for recursive call */ return array_map(__FUNCTION__, $d); } else { // Return array return $d; } }
🌐
GitHub
ocramius.github.io › blog › fast-php-object-to-array-conversion
Fast PHP Object to Array conversion
<?php class Foo { public $bar = 'barValue'; protected $baz = 'bazValue'; private $tab = 'tabValue'; } $foo = new Foo(); $arrayFoo = (array) $foo; var_dump($arrayFoo); ... array(3) { ["bar"]=> string(8) "barValue" ["*baz"]=> string(8) "bazValue" ["Footab"]=> string(8) "tabValue" } Weird, so $baz is copied to array key '*baz' and $tab is copied to Footab...