2014-10-28 54 views
0

为什么我在使用此函数时遇到未识别的索引错误,它输出的代码是我想要的,但它也将错误引入页面?身份不明的索引错误

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”, “值”:[{ “×”:0, “Y”:4},{ “×”: 1, “Y”:3},{ “×”:2 “Y”:2},{ “×”:3, “Y”:1},{ “×”:4, “Y”:1} ]},{ “键”: “OW2”, “值”:[{ “×”:0, “Y”:4},{ “×”:1, “y” 的:2},{ “×”: 2, “Y”:1},{ “×”:3, “Y”:3},{ “×”:4, “Y”:2}]},{ “键”: “OW3”,“值“:[{” × “:0,” Y “:4},{” × “:1,” Y “:5},{” × “:2”,Y “:1},{” ד: 3, “Y”:2},{ “×”:4, “Y”:1}]}]

和错误这些

说明:未定义指数:OW1在C:\瓦帕\ WWW \ multibar.html.php 24行

说明:未定义指数:OW2在C:\瓦帕\ WWW \ multibar.html.php上线24

说明:未定义指数:OW3在C:\瓦帕\ WWW \ multibar.html.php上第24行

回答

0

通知被抛出,因为您正在将元素附加到尚未定义的变量。 在PHP中,这实际上不是问题,因为PHP只是将变量转换为任何需要的变量。

为了消除这个通知一定要初始化所有的变量:

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

完美工作,非常感谢! – Engl12 2014-10-28 19:09:23