0

I have an array which looks like this

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

I want to add second value if 1st value are same. i have tried array_unique($array) but not able to add second value if first values are same.

So the output i expected is

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

I tried as below but not able to solve

$array=array_unique($array)
and 
        foreach ($array as $unique){
            if( in_array( $unique['Country'] ,$array) )
            {
                print_r ($unique['Country']);
            }
        }

Can you help me out?

8 Answers 8

4

you can use array_reduce, like:

$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));
Sign up to request clarification or add additional context in comments.

Comments

2

You can do a classic foreach loop for this:

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

This will result to:

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
        )

)

Comments

2

you can make unique using this method

array_unique($associativeArray,SORT_REGULAR);

Define an associative array $associativeArray with name and age keys.

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

Comments

1

You can simply iterate through the first array and summarize them your self:

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

Demo: https://3v4l.org/ZuUoL

Comments

0

You can use PHP function

    in_array()

try this

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

Comments

0

Try This out. I Hope this will help you out :

$country_users = array();
foreach ($array as $each) {
    $value = $each['users'];
    if (array_key_exists($each['Country'], $country_users)) {
        $prev_users = $country_users[$each['Country']];
        $value = $each['users'] + $prev_users;
    }
    $country_users[$each['Country']] = $value;
}
unset($array);
foreach ($country_users as $country=>$users) {
    $array[] = array('Country' => $country, 'users' => $users);
}
echo '<pre>';
print_r($array);

Comments

0

you will get your result by this code:

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

Comments

0

You may simple use array_merge_recursive(), array_unique(), in_array() functions. As you saw advice above. But my advice is change array structure (if it's possible) to have associative array with country name as key and count as value. It would be more simple and clear to operate and understand.

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

You may change array structure later for response as you need. But operating with key-value structure helps you prevent a lot of troubles, I believe.

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.