dspym82000 2014-06-21 10:21 采纳率: 100%
浏览 38
已采纳

PHP搜索多维关联数组

I am trying to search a multi-dimensional associative array and change a value after searching. Here is what my array looks like

 $arr=Array ( [0] => Array ( [1] => 
    Array ( [keyword] => 2014 
            [count] => 97 
            [percent] => 4.91 )))

So what I am trying to do is to search for keyword and if found then increase the count on that particular index where keyword was found.

So I am trying to do something like:

if(in_array("2014", $arr))
{
//then add 100 to count that is 100+97

}

So what will be the best way to go about this.

Note: I am trying to search a value in the array and if found then update the value of count key on that particular index. The last part is as important as first.

Ahmar

  • 写回答

3条回答 默认 最新

  • doushang7209 2014-06-21 11:00
    关注

    you can use that code:

    $arr = Array(
        0 => Array(
            1 => Array(
                'keyword' => 2014,
                'count' => 97,
                'percent' => 4.91
            )
        )
    );
    
    foreach ($arr as &$arr1) {
    
        foreach ($arr1 as &$arr2) {
    
            if (2014 == $arr2['keyword']) {
                $arr2['count'] += 100;
            }
    
        }
    }
    
    unset($arr2, $arr1);
    

    Result:

    array(1) {
      [0]=>
      array(1) {
        [1]=>
        array(3) {
          ["keyword"]=>
          int(2014)
          ["count"]=>
          int(197)
          ["percent"]=>
          float(4.91)
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?