PHP搜索多维关联数组 [英] PHP Searching multidimensional associative array

查看:88
本文介绍了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.

注意:我正在尝试在数组中搜索一个值,如果找到该值,然后更新该特定索引上的count键的值.最后一部分与第一部分一样重要.

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

推荐答案

您可以使用该代码:

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

结果:

array(1) {
  [0]=>
  array(1) {
    [1]=>
    array(3) {
      ["keyword"]=>
      int(2014)
      ["count"]=>
      int(197)
      ["percent"]=>
      float(4.91)
    }
  }
}

这篇关于PHP搜索多维关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆