1

Actually i am not able to convert php object to array and specially when i have other access specifier. for example:

<?php

class Foo
{
    public $bar = 'barValue';
    protected $baz = 'bazValue';
    private $tab = 'tabValue';
}

$foo = new Foo();

$arrayFoo = (array) $foo;

echo "<pre>";
var_dump($arrayFoo);

and output is:

array(3) {
  ["bar"]=>
  string(8) "barValue"
  ["*baz"]=>
  string(8) "bazValue"
  ["Footab"]=>
  string(8) "tabValue"
}

so i am not able to get the key with its name, it automatic added * (for protected) and class name (for private),

manish1706
  • 1,571
  • 24
  • 22

1 Answers1

2

You can use function mentioned in comments for get_object_vars PHP documentation:

function obj2array ( &$Instance ) {
    $clone = (array) $Instance;
    $rtn = array ();
    $rtn['___SOURCE_KEYS_'] = $clone;

    while ( list ($key, $value) = each ($clone) ) {
        $aux = explode ("\0", $key);
        $newkey = $aux[count($aux)-1];
        $rtn[$newkey] = &$rtn['___SOURCE_KEYS_'][$key];
    }

    return $rtn;
}
aslawin
  • 1,981
  • 16
  • 22