关联数组未找到指定的索引

时间:2021-05-14 21:18:51

I am having some problem with associative array. When I check the array $keywordstoupdate does return a value but when it reaches to echo it say Keyword is undefined. However, print_r() prints it and everything is fine from that perspective. but when I try to echo then the Keyword is missing.

我对关联数组有一些问题。当我检查数组$ keywordstoupdate确实返回一个值但是当它到达echo时它说Keyword是未定义的。但是,print_r()打印它,从那个角度看一切都很好。但是当我尝试回声时,关键字丢失了。

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>

The result of print_r()

print_r()的结果

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

    )

1 个解决方案

#1


It looks like you're creating a three-dimensional array accidentally, while your loop is expecting a two-dimensional array.

看起来你正在意外地创建一个三维数组,而你的循环期待一个二维数组。

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

尝试更改:$ keywordstoupdate [] = getkeywords($ mysqli,$ someid);

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

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

#1


It looks like you're creating a three-dimensional array accidentally, while your loop is expecting a two-dimensional array.

看起来你正在意外地创建一个三维数组,而你的循环期待一个二维数组。

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

尝试更改:$ keywordstoupdate [] = getkeywords($ mysqli,$ someid);

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

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