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),

2
  • as you have defines variables protected and private thats why its giving * , Foot in array indexes... Commented Jun 10, 2016 at 7:20
  • 3
    That's a feature, not a bug Commented Jun 10, 2016 at 7:28

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.