PHP关联数组与唯一值

时间:2018-01-25 06:00:23

标签: php multidimensional-array associative-array

我有一个看起来像这样的数组

$array=Array ( [0] => Array ( [Country] => United Arab Emirates [users] => 2 )
        [1] => Array ( [Country] => Albania [users] => 1 ) 
        [2] => Array ( [Country] => Armenia [users] => 4 ) 
        [3] => Array ( [Country] => Argentina [users] => 12 )
        [4] => Array ( [Country] => United Arab Emirates [users] => 3 ) 
        [5] => Array ( [Country] => Austria [users] => 1 ) 
        [6] => Array ( [Country] => Austria [users] => 8 ) 
        [7] => Array ( [Country] => Austria [users] => 1 ) )

如果第一个值相同,我想添加第二个值。 我尝试过array_unique($ array)但如果第一个值相同则无法添加第二个值。

所以我预期的输出是

Array ( [0] => Array ( [Country] => United Arab Emirates [users] => 5 )
        [1] => Array ( [Country] => Albania [users] => 1 ) 
        [2] => Array ( [Country] => Armenia [users] => 4 ) 
        [3] => Array ( [Country] => Argentina [users] => 12 )
        [4] => Array ( [Country] => Austria [users] => 10 ))

我尝试如下,但无法解决

$array=array_unique($array)
and 
        foreach ($array as $unique){
            if( in_array( $unique['Country'] ,$array) )
            {
                print_r ($unique['Country']);
            }
        }
你可以帮帮我吗?

8 个答案:

答案 0 :(得分:4)

您可以使用array_reduce,例如:

$sum = array_reduce($data, function ($a, $b) {
    isset($a[$b['Country']]) ? $a[$b['Country']]['users'] += $b['users'] : $a[$b['Country']] = $b;  
    return $a;
});


echo "<pre>"; print_r(array_values($sum));

答案 1 :(得分:2)

您可以为此执行经典 foreach循环:

$result = array();

foreach( $array as $value ){
    if( !isset( $result[ $value[ "Country" ] ] ) ) $result[ $value[ "Country" ] ] = $value;
    else $result[ $value[ "Country" ] ][ "users" ] += $value[ "users" ];
}

$result = array_values($result);

这将导致:

Array
(
    [0] => Array
        (
            [Country] => United Arab Emirates
            [users] => 5
        )

    [1] => Array
        (
            [Country] => Albania
            [users] => 1
        )

    [2] => Array
        (
            [Country] => Armenia
            [users] => 4
        )

    [3] => Array
        (
            [Country] => Argentina
            [users] => 12
        )

    [4] => Array
        (
            [Country] => Austria
            [users] => 10
        )

)

答案 2 :(得分:1)

你可以简单地遍历第一个数组并将它们总结为你自己:

$array = [
    [ "Country" => "United Arab Emirates", "users" => 2 ],
    [ "Country" => "Albania", "users" => 1 ], 
    [ "Country" => "Armenia", "users" => 4 ], 
    [ "Country" => "Argentina", "users" => 12 ],
    [ "Country" => "United Arab Emirates", "users" => 3 ], 
    [ "Country" => "Austria", "users" => 1 ], 
    [ "Country" => "Austria", "users" => 8 ], 
    [ "Country" => "Austria", "users" => 1 ], 
 ];

 $new = [];

 foreach ($array as $item) {
     // Check if we already have added the country to the new array
     if (empty($new[$item['Country']])) {
         // The country doesn't exist, add it
         $new[$item['Country']] = ['Country' => $item['Country'], 'users' => 0]; 
     }

     // Add the amount of users
     $new[$item['Country']]['users'] += $item['users'];
 }

 // Since we used the country as key, we can use array_values() 
 // to get it as an array with numeric indexes again.
 $new = array_values($new);

演示:https://3v4l.org/ZuUoL

答案 3 :(得分:0)

您可以使用PHP功能

    in_array()

试试这个

  $array=array();
  foreach($array as $k=>$v){
  foreach($v as $key=>$value){
    if(!in_array($value, $array)){
    $array[]=$value;
    }
  }
 }

答案 4 :(得分:0)

试一试。我希望这会帮助你:

Failed to compile.

multi ./node_modules/react-scripts/config/polyfills.js ./node_modules/react-dev-utils/webpackHotDevClient.js ./src/index.js
Module not found: Can't resolve 'D:\' in 'D:\!!!DESKTOP\React-test\test-react'

答案 5 :(得分:0)

您将通过以下代码获得结果:

$request = array();
    foreach ($yourArray as $key => $value) {
      if (!in_array($value['country'], $request)) {
        $request[] = $value['country'];
      }
      else {
        unset($yourArray[$key]);
      }
    }
    print_r($yourArray);

答案 6 :(得分:0)

您可以简单地使用array_merge_recursive()array_unique()in_array()个功能。正如你在上面看到的建议。但我的建议是更改数组结构(如果可能的话)将关联数组与国家/地区名称作为键并计为值。操作和理解会更简单明了。

$array = [
    'Austria' => 12,
];

您可以根据需要稍后更改阵列结构以进行响应。但是我认为,使用键值结构进行操作有助于防止很多麻烦。

答案 7 :(得分:0)

您可以使用此方法制作独一无二的

array_unique($associativeArray,SORT_REGULAR);

使用名称和年龄键定义关联数组 $ associativeArray

 $associativeArray[] = array("name" => "pankaj Singh","no"=>4);
 $associativeArray[] = array("name" => "pankaj Singh","no"=>24);