0

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.

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()

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

    )
3
  • What variable is that print_r() displaying? Commented May 13, 2015 at 19:46
  • did you printed $keywordstoupdate? Commented May 13, 2015 at 19:46
  • Yes... That is the result of $keywordstoupdate.. and by the way I think I have been answered.. Commented May 13, 2015 at 19:48

1 Answer 1

3

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);

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.