Skip to content

Instantly share code, notes, and snippets.

@fedir
Last active January 12, 2018 14:35
Show Gist options
  • Save fedir/7871162 to your computer and use it in GitHub Desktop.
Save fedir/7871162 to your computer and use it in GitHub Desktop.
Conversion of an object to array with var_export / eval // via http://www.kormoc.com/2012/11/18/convert-a-php-object-to-an-array/
<?php
function toArray($object) {
$dump = '$dump = '.var_export($object, true);
$dump = preg_replace('/object\([^\)]+\)#[0-9]+ /', 'array', $dump);
$dump = preg_replace('/[a-zA-Z_]+::__set_state/', '', $dump);
$dump = str_replace(':protected', '', $dump);
$dump = str_replace(':private', '', $dump);
$dump .= ';';
eval($dump);
return $dump;
}
Copy link

ghost commented Mar 14, 2014

Thank you so much for writing this man. Saved my but there. If there was some way to tip you I definitely would!

@dbunic
Copy link

dbunic commented Jan 12, 2018

Hi,
I have to add dash to the REGEX because Oracle PHP driver returns name with dash in it:

  array (
    'ptip_sif' => '4',
    'pptip_opis' => 
    OCI-Lob::__set_state(array(
       'descriptor' => NULL,
    )),
  ),

Instead of:

$dump = preg_replace('/[a-zA-Z_]+::__set_state/', '', $dump);

... here is REGEX that contains dash "\-"

$dump = preg_replace('/[a-zA-Z_\-]+::__set_state/', '', $dump);

Regards,
Darko

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment