[英]PHP associative array merge with unique values
我有一個看起來像這樣的數組
$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']);
}
}
你能幫我嗎?
您可以使用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));
您可以為此執行經典的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
)
)
您可以簡單地遍歷第一個數組並自己總結它們:
$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
您可以使用此方法制作獨特的
array_unique($associativeArray,SORT_REGULAR);
使用名稱和年齡鍵定義關聯數組$associativeArray 。
$associativeArray[] = array("name" => "pankaj Singh","no"=>4);
$associativeArray[] = array("name" => "pankaj Singh","no"=>24);
您可以使用 PHP 函數
in_array()
試試這個
$array=array();
foreach($array as $k=>$v){
foreach($v as $key=>$value){
if(!in_array($value, $array)){
$array[]=$value;
}
}
}
試試這個。 我希望這會幫助你:
$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);
您將通過以下代碼獲得結果:
$request = array();
foreach ($yourArray as $key => $value) {
if (!in_array($value['country'], $request)) {
$request[] = $value['country'];
}
else {
unset($yourArray[$key]);
}
}
print_r($yourArray);
您可以簡單地使用array_merge_recursive()
、 array_unique()
、 in_array()
函數。 正如你在上面看到的建議。 但我的建議是更改數組結構(如果可能),將關聯數組與國家名稱作為鍵並作為值計數。 操作和理解會更簡單明了。
$array = [
'Austria' => 12,
];
您可以稍后根據需要更改數組結構以進行響應。 但我相信,使用鍵值結構操作可以幫助您避免很多麻煩。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.