简体   繁体   English

致命错误:无法将字符串偏移量用作数组

[英]Fatal error: Cannot use string offset as an array

Fatal error: Cannot use string offset as an array in C:\\xampp\\htdocs\\includes\\categories\\categories.php on line 12 致命错误:无法在第12行的C:\\ xampp \\ htdocs \\ includes \\ categories \\ categories.php中将字符串偏移量用作数组

$categories[$parent][] = $row;

categories.php Categories.php

    <?php

$sql = "SELECT catid, catname, parentid FROM categories";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res)) {
    $parent = intval($row['parentid']);
    if (!isset($categories[$parent])) {
        $categories[$parent] = array();
    }
    $categories[$parent][] = $row;
}
    ?>
    <table border="0" cellpadding="10" cellspacing="0">
    <tr>
        <td valign="top">
    <?php
    $category_string = "";
    function build_categories_options($parent, $categories, $level) {
        global $category_string;
        if (isset($categories[$parent]) && count($categories[$parent])) {
            $level .= " - ";
            foreach ($categories[$parent] as $category) {
                $opt_value = substr($level.$category['catname'],3);
                $category_string .= '<option value=""></option><option value="'.$category['catid'].'">'.$opt_value.'</option>';
                build_categories_options($category['catid'], $categories, $level);
            }
            $level = substr($level, -3);
        }
        return $category_string;
    }
    $category_options = build_categories_options(0, $categories, '');
    $category_options = '<select class="chosen" name="categories" id="categories">'.$category_options.'</select>';
    echo $category_options; 
    ?>
</td>

After I insert post With category This Error Will Show ?? 在我插入带有类别的帖子后,将显示此错误?

I don't see where $categories is initialized, but I'm betting that it's not an array when you enter your while loop, and that's why you're getting an error. 我看不到$categories在哪里初始化,但是我敢打赌,当您进入while循环时,它不是一个数组,这就是为什么会出错的原因。 Try doing this for your while loop: 尝试为您的while循环这样做:

// initialize $categories to make sure it is an array
$categories = array();
while ($row = mysql_fetch_assoc($res)) {
    $parent = intval($row['parentid']);
    $categories[$parent][] = $row;
}

You don't need to explicitly initialize $categories[$parent] ...this will be done automatically when you call $categories[$parent][] = $row; 您不需要显式初始化$categories[$parent] ...这将在您调用$categories[$parent][] = $row;时自动完成$categories[$parent][] = $row; . We know that it will start out blank because we started with an empty array before the loop. 我们知道它将开始为空,因为我们在循环之前从一个空数组开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:不能将字符串偏移量用作数组? - Fatal error: Cannot use string offset as an array? “不能将字符串偏移量用作数组”致命错误 - “Cannot use string offset as an array” Fatal Error 致命错误:不能将字符串偏移用作数组 - Fatal error: Cannot use string offset as an array PHP 7 致命错误:未捕获错误:无法使用字符串偏移量作为数组 - PHP 7 Fatal error: Uncaught Error: Cannot use string offset as an array 致命错误:未捕获错误:无法使用字符串偏移量作为数组 - Fatal error: Uncaught Error: Cannot use string offset as an array 使用变量时:致命错误:不能将字符串偏移量用作数组 - when using variable: Fatal error: Cannot use string offset as an array PHP致命错误:无法将字符串偏移量用作数组 - PHP Fatal error: Cannot use string offset as an array 致命错误:不能将字符串偏移用作数组 - 从JSON响应中解析字符串 - Fatal error: Cannot use string offset as an array - parsing string from JSON response 致命错误:无法将字符串偏移量用作数组-比较数组值的正确方法是什么 - Fatal error: Cannot use string offset as an array - What the right way to compare array value 越来越令人讨厌的错误:“致命错误:无法在…中使用字符串偏移量作为数组” - getting really annoying error: “Fatal error: Cannot use string offset as an array in…”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM