亚洲免费乱码视频,日韩 欧美 国产 动漫 一区,97在线观看免费视频播国产,中文字幕亚洲图片

      1. <legend id="ppnor"></legend>

      2. 
        
        <sup id="ppnor"><input id="ppnor"></input></sup>
        <s id="ppnor"></s>

        PHP Fatal error: Cannot use object of type stdClass as array in錯誤

        字號:


            下面一起來看看在php開發(fā)中碰到PHP Fatal error: Cannot use object of type stdClass as array in錯誤問題的解決辦法吧。
            普通的數(shù)組出現(xiàn)如下錯誤
            代碼如下
            <?php
            Array (
            [0] => stdClass Object (
            [id] => 1
            [title] =>精彩推薦
            [size] => 280*150
            [pic] => ./uploadfiles/201402160422.jpg
            [state] => 0 )
            [1] => stdClass Object (
            [id] => 2
            [title] =>企業(yè)要聞
            [size] => 280*150
            [pic] => ./uploadfiles/201402160533.jpg
            [state] => 0 )
            )
            ?>
            后來百度搜索一個關(guān)于差不多的問題,但對方是json數(shù)據(jù)
            php再調(diào)用json_decode從字符串對象生成json對象時,如果使用[]操作符取數(shù)據(jù),會得到下面的錯誤:
            Cannot use object of type stdClass as array
            產(chǎn)生原因:
            代碼如下
            $res = json_decode($res);
            $res['key']; //把 json_decode() 后的對象當(dāng)作數(shù)組使用。
            解決方法(2種):
            1、使用 json_decode($d, true)。就是使json_decode 的第二個變量設(shè)置為 true。
            2、json_decode($res) 返回的是一個對象, 不可以使用 $res['key'] 進行訪問, 換成 $res->key 就可以了。
            好了現(xiàn)在回到我們原問題發(fā)現(xiàn)
            原來是不能直接用[]來顯示導(dǎo)致的,上面代碼的輸出是用的:$pic[0][title],改為$pic[0]->title后正常了