dsfvsdfv23599 2011-06-07 16:37
浏览 230
已采纳

替换数组中的值不起作用

I'm going crazy, spent a couple of hours trying different methods in replace values in arrays, but I can't get it to work.

foreach($potentialMatches as $potentialKey)
{
  $searchKeywordQuery = "SELECT keyword, id FROM picture WHERE id='$potentialKey'";
  $searchKeywords = mysql_query($searchKeywordQuery) or die(mysql_error());
  while ($searchKeyWordsRow = mysql_fetch_array($searchKeywords))
  {
    $keyword = $searchKeyWordsRow['keyword'];
    $pictureKeywordArray[$searchKeyWordsRow['id']]['keywords'] = explode(",", $keyword);
    $pictureKeywordArray[$searchKeyWordsRow['id']]['match'] = 4;
  }
}
foreach($pictureKeywordArray as $key = > $picValue)
{
  foreach($picValue['keywords'] as $key = > $picIdValue)
  {
    if ($picIdValue == $searchIdKey)
    {
      echo $picValue['match'];
      $picValue['match']++;
      echo $picValue['match'];
    }
  }
}
foreach($pictureKeywordArray as $key = > $picValue)
{
  echo $picValue['match'];
}

I'm novice as you can see, When I echo the picValue['match'] in the foreach loop it gives me a correct value after "++". But then below when I call the array again it gives me the value of 4 instead of 5 as intended. Thanks in advance for any help with this.

  • 写回答

3条回答 默认 最新

  • dougou2937 2011-06-07 16:39
    关注

    In that second foreach you need to call it by reference:

    foreach($pictureKeywordArray as $key => &$picValue) 
    {                                       //^-- `&` makes it by reference
      foreach($picValue['keywords'] as $key => $picIdValue)
      {
        if ($picIdValue == $searchIdKey)
        {
          echo $picValue['match'];
          $picValue['match']++; //now updates what you want it to update
          echo $picValue['match'];
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题