在文件php中为foreach()提供的参数无效

时间:2015-05-28 16:40:16

标签: php foreach

我在下面的代码中出现以下错误。

  

警告:为foreach()提供的参数无效   第263行/admin/controller/module/megamenu.php列表已更新   15-05-27 23:25:14!

第263行foreach ($jsonArray as $subArray) {

我检查了另一台服务器,并没有出现此错误(php5.4),但我的服务器与php5.3,php5.5出现。缺少什么?

if (isset($_GET['jsonstring'])) {
        if($this->validate()){
            $jsonstring = $_GET['jsonstring'];
            $jsonDecoded = json_decode(html_entity_decode($jsonstring));

            function parseJsonArray($jsonArray, $parentID = 0) {
                $return = array();
                foreach ($jsonArray as $subArray) {
                    $returnSubSubArray = array();
                    if (isset($subArray->children)) {
                        $returnSubSubArray = parseJsonArray($subArray->children, $subArray->id);
                    }
                    $return[] = array('id' => $subArray->id, 'parentID' => $parentID);
                    $return = array_merge($return, $returnSubSubArray);
                }

                return $return;
            }


            $readbleArray = parseJsonArray($jsonDecoded);

            foreach ($readbleArray as $key => $value) {
                if (is_array($value)) {
                    $this->model_menu_megamenu->save_rang($value['parentID'], $value['id'], $key, $data['active_module_id']);
                }   
            }

            die("The list was updated ".date("y-m-d H:i:s")."!");

        } else {
            die($this->language->get('error_permission'));
        }
    }

2 个答案:

答案 0 :(得分:2)

更改

$jsonDecoded = json_decode(html_entity_decode($jsonstring)); 

$jsonDecoded = json_decode(html_entity_decode($jsonstring),true);

$jsonDecoded将是一个数组

答案 1 :(得分:-1)

我认为在某些情况下,当我们使用php中的json数据时,我们必须使用implode和explode函数,在解码json数据后将其转换为数组。