未识别的索引错误

时间:2014-10-28 19:01:51

标签: php

为什么我使用此函数时会出现未识别的索引错误,它输出的代码是我想要的,但是它也会在页面中抛出错误?

if($result) {
    $jsonData = convert($result);
}

function convert($result) {
$i = 0;
    $intermediate = array();

    while($rows = mysqli_fetch_assoc($result)) {
        $key = $rows['POS'];
        $x = $i;
        $y = $rows['COUNT'];
        $intermediate[$key][] = array('x' => count($intermediate[$key]), 'y' => $y);
        $i++;
    }


   $output = array();

    foreach($intermediate as $key => $values) {
        $output[] = array(
            "key" => $key,
            'values' => $values
        );
    }

    return json_encode($output, JSON_NUMERIC_CHECK);

它返回的数据是

[{"键":" OW1""值":[{" X":0," Y& #34:4},{" X":1," Y":3},{" X":2" Y&#34 ; 2},{" X":3," Y":1},{" X&#34:4," Y&#34 ;: 1}]},{"键":" OW2""值":[{" X":0," ý&#34:4},{" X":1," Y":2},{" X":2" Y&# 34;:1},{" X":3," Y":3},{" X&#34:4," Y" :2}]},{"键":" OW3""值":[{" X":0,&#34 ; Y&#34:4},{" X":1," Y":5},{" X":2" Y& #34;:1},{" X":3," Y":2},{" X&#34:4," Y&#34 ;:1}]}]

错误就是这些

注意:未定义的索引:第24行的C:\ wamp \ www \ multibar.html.php中的OW1

注意:未定义的索引:第24行的C:\ wamp \ www \ multibar.html.php中的OW2

注意:未定义的索引:第24行的C:\ wamp \ www \ multibar.html.php中的OW3

1 个答案:

答案 0 :(得分:0)

抛出通知是因为您将元素附加到尚未定义的变量。 在PHP中,这不是一个问题,因为PHP只是将变量转换为它需要的任何东西。

要消除此通知,请务必初始化所有变量:

if (!isset($intermediate[$key])) $intermediate[$key] = array();
$intermediate[$key][] = array('x' => count($intermediate[$key]), 'y' => $y);