2015-05-13 23 views
0

我在關聯數組中遇到了一些問題。當我檢查數組$keywordstoupdate是否返回一個值,但是當它達到echo時,它說Keyword未定義。然而,print_r()打印它,從這個角度來看一切都很好。但是當我嘗試回顯時,Keyword丟失。關聯數組未找到指定索引

function getkeywords($mysqli, $someid) 
{ 

    $keywords=array(); 
    $query='select Keyword from keywords where someId=?'; 
    $stmt= $mysqli->stmt_init(); 
    $stmt->prepare($query); 
    $stmt->bind_param('i', $someid); 
    $stmt->execute(); 
    $stmt->bind_result($Keyword); 
    while($stmt->fetch()) 
    { 

     $keywords[]= array("Keyword" => $Keyword); 
    } 

    return $keywords; 
} 

    $keywordstoupdate[]=getkeywords($mysqli, $someid); 

    <textarea id='textarea_keywords' name='keywords'> 
    <?php 
    if(count($keywordstoupdate)>0){ 
    for($i=0; count($keywordstoupdate)>$i; $i++){ 
    echo ($keywordstoupdate[$i]['Keyword']." "); 
    } 
    } ?></textarea> 

print_r()

 Array 
    (
     [0] => Array 
     (
       [Keyword] => asdf 
     ) 

    ) 
+0

什麼變量是'的print_r()'顯示? –

+0

是否打印過$ keywordstoupdate? –

+0

是的...這是'$ keywordstoupdate'的結果..順便說一句,我想我已經得到了答案.. – whatever

回答

3

結果看起來你正在創建一個三維數組意外,而你的循環期待一個二維數組。

嘗試改變:
$keywordstoupdate[]=getkeywords($mysqli, $someid);

要:
$keywordstoupdate=getkeywords($mysqli, $someid);

+0

Ouch !!!!!!!!!!! – whatever

+0

Hola !!!!!!!!織補物...固定! – whatever