PHP:cast到(array)和return-type:array不一样? [英] PHP: cast to (array) and return-type: array is not the same?

查看:216
本文介绍了PHP:cast到(array)和return-type:array不一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中遇到以下问题:

I have following problem in PHP:

print_r()说是相同的, gettype()说同样的类型,但最后一个输出不适用于这两种情况,尽管它们应该是一样的!

print_r() says it's the same, gettype() says same type, but the last output works not for both cases although they should be the same!

对我很奇怪。

代码和输出:

$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]);

输出:

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 out2 应该产生相同的结果,但不是。

out1 and out2 should produce the same results but don't.

也许任何人都有一个提示

Perhaps anybody has a hint for me?

推荐答案

有几个PHP错误:

  • http://bugs.php.net/bug.php?id=45346
  • http://bugs.php.net/bug.php?id=51635
  • http://bugs.php.net/bug.php?id=46758

同样的事情发生在这里:

The same things happens here:

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

这是因为0用作字符串数组键,而$ arr [0]为整数数组键。它在PHP文档中简单说明:整数属性是不可访问的 link )。

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

这篇关于PHP:cast到(array)和return-type:array不一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆