views:

69

answers:

1

Hi! I have following problem in PHP:

print_r says its the same, gettype says same type.. but the last output works not for both cases... although they should be the same!!

this looks very strange for me...

code and output:

$docdatau = get_object_vars(json_decode($docdata));
$docdatau2 = (array)json_decode($docdata);

echo "1\n";
echo gettype($docdatau);
echo "\n";
echo "--------------------------------------\n";
print_r($docdatau);
echo "--------------------------------------\n";

echo "2\n";
echo gettype($docdatau2);
echo "\n";
echo "--------------------------------------\n";
print_r($docdatau2);

echo "out1\n";
echo "--------------------------------------\n";
print_r($docdatau[0]);
echo "out2\n";
echo "--------------------------------------\n";
print_r($docdatau2[0]);

The output:

1
array
--------------------------------------
Array
(

    [0] => stdClass Object
        (
            [produkt] => Produkt 2
            [laufzeit] => 24
            [addtext] => sdsd
            [provision] => 39
        )

    [1] => stdClass Object
        (
            [produkt] => Produkt 1
            [laufzeit] => 
            [addtext] => 
            [provision] => 0
        )

)
--------------------------------------
2
array
--------------------------------------

Array
(

    [0] => stdClass Object
        (
            [produkt] => Produkt 2
            [laufzeit] => 24
            [addtext] => sdsd
            [provision] => 39
        )

    [1] => stdClass Object
        (
            [produkt] => Produkt 1
            [laufzeit] => 
            [addtext] => 
            [provision] => 0
        )

)
out1
--------------------------------------
stdClass Object
(
    [produkt] => Produkt 2
    [laufzeit] => 24
    [addtext] => sdsd
    [provision] => 39
)
out2
--------------------------------------
--------------------------------------

out1 and out2 should produce the same results but doesnt..

perhaps anybody has a hint for me.. thank you.

greetings andreas k.

+1  A: 

There are several PHP bugs about it:

The same things happens here:

$obj->{0} = "hello";
$arr = (array)$obj;
echo $arr[0];

It happens because the "0" is used as string array key, whereas $arr[0] searches for the integer array key. It is documented in the PHP documentation simply by stating: integer properties are unaccessible (link).

Sjoerd
that should be the answer. thank you.i think php is an ugly language. you can't expect such a behaviour. and instead of fixing it, it becomes a "known issue that isn't a bug". i hope such things don't appear too often in PHP.
Andreas K.
Don't count on it. This is fairly typical behavior.
Sjoerd
what behaviour exactly do you mean? that bugs are declared as known issues? or that the same content of 2 variables can't be used the same way depending on where they came from?so how to find out if an array is a "real" array or a casted object? gettype and print_r are both the same in both cases. wheres the difference?an integer key is not accessible.. hm.. does it exist in an other way that an accessible integer key exists? who can't access that key? print_r could read the key and the cast operator could write the key. so it exists for me. i think this is clearly a bug..
Andreas K.
That bugs are declared as known issues. You can access the key using a foreach loop.
Sjoerd
ok so how can i find out if an array is a "real" array or a casted object?
Andreas K.
i would be happy if the foreach loop is not the only answer...
Andreas K.