PHP error converting an object to arrays

I had this question before, and it was concluded that it was a mistake in 5.2.5. Well, it is still broken in 5.2.6, at least for me:

Please let me know if it is broken or working for you:

$obj = new stdClass();
$obj->{"foo"} = "bar";
$obj->{"0"} = "zero";
$arr = (array)$obj;

//foo -- bar
//0 --    {error: undefined index}
foreach ($arr as $key=>$value){
    echo "$key -- " . $arr[$key] . "\n";
}

Any ways to “fix” an array after it has been ported from stdClass?

+3
source share
3 answers

Definitely seems a mistake to me (PHP 5.2.6).

You can fix the array as follows:

$arr = array_combine(array_keys($arr), array_values($arr));

Reported in this bug report , but marked as dummy ... the documentation says:

Keys is a member variable names with a few notable exceptions: integer properties are not available;

+2
source

, phps .

function noopa( $a ){ return $a; }
$arr = array_map('noopa', $arr ); 
$arr[0]; # no error! 

, .

, , array_merge , , ?

, , , , , homebrew array_merge.

, php perl , , , . !.

- - , - - .

+1

RoBorg.. :)

Here's another solution, not sure if it’s faster or not:

unserialize(serialize($arr));
0
source

All Articles