PHP7:合并两个数组,其中一个是键,第二个是值(整数),对重复键的值求和
PHP7: Combine two arrays, where one is key and second is value (integer), sum values for duplicate keys
我在 PHP7 中有 2 个数组:
$Array1 = ["bus","bus","int"];
$Array2 = [2,18,10];
其中每个索引的 $Array1 是 key,$Array2 是 value。
我需要将重复键的两个值和求和值组合起来,例如获得以下输出:
$Array3 = ["bus" => 20, "int" => 10];
谢谢!
我会成功的。我希望它对您有所帮助:您可以通过使用额外的数组来获得所需的输出,首先,计算您的数组元素,这将满足您的条件。这个简单的检查
$Array1[$x] == $Array1[$x+1]
将检查数组连续地址是否具有相同的值。 (条件是数组必须排序),
另一个值是这样的:
$arr[$Array1[$x]] // assign a key
这是为了让它关联数组:
$Array1 = ["bus","bus","int"];
$Array2 = [2,18,10];
$arr = [];
for ($x = 0; $x < count($Array1); $x++) {
if($Array1[$x] == $Array1[$x+1])
{
$arr[$Array1[$x]] = $Array2[$x] + $Array2[$x+1];
$x++;
}
else
{
$arr[$Array1[$x]] = $Array2[$x];
}
}
输出是:
Array ( [bus] => 20 [int] => 10 )
您需要的代码就是这么简单:
// The input arrays
$Array1 = ['bus', 'bus', 'int'];
$Array2 = [2, 18, 10];
// Build the result here
$Array3 = [];
// There is no validation, the code assumes that $Array2 contains
// the same number of items as $Array1 or more
foreach ($Array1 as $index => $key) {
// If the key $key was not encountered yet then add it to the result
if (! array_key_exists($key, $Array3)) {
$Array3[$key] = 0;
}
// Add the value associate with $key to the sum in the results array
$Array3[$key] += $Array2[$index];
}
print_r($Array3);
它的输出:
Array
(
[bus] => 20
[int] => 10
)
这个可以工作:
$Array1 = ['bus', 'bus', 'int'];
$Array2 = [2, 18, 10];
# Let $Array3 be the required results array.
$Array3 = [];
for($j = 0; $j < count($Array1); $j += 1){
$k = $Array1[$j];
/*If the key already exists in $Array3 , add to it the value in the present key,
else just enter it as a new element. */
if(array_key_exists($k,$Array3)){ $Array3[$k] = $Array3[$k] + $Array2[$j];}
else {
# But first check that the length of $Array2 is not exceeded
if($j <= count($Array2)){
$Array3[$k] = $Array2[$j];
}
}
}
print_r($Array3);
# gives: Array ( [bus] => 20 [int] => 10 )
我在 PHP7 中有 2 个数组:
$Array1 = ["bus","bus","int"];
$Array2 = [2,18,10];
其中每个索引的 $Array1 是 key,$Array2 是 value。
我需要将重复键的两个值和求和值组合起来,例如获得以下输出:
$Array3 = ["bus" => 20, "int" => 10];
谢谢!
我会成功的。我希望它对您有所帮助:您可以通过使用额外的数组来获得所需的输出,首先,计算您的数组元素,这将满足您的条件。这个简单的检查
$Array1[$x] == $Array1[$x+1]
将检查数组连续地址是否具有相同的值。 (条件是数组必须排序),
另一个值是这样的:
$arr[$Array1[$x]] // assign a key
这是为了让它关联数组:
$Array1 = ["bus","bus","int"];
$Array2 = [2,18,10];
$arr = [];
for ($x = 0; $x < count($Array1); $x++) {
if($Array1[$x] == $Array1[$x+1])
{
$arr[$Array1[$x]] = $Array2[$x] + $Array2[$x+1];
$x++;
}
else
{
$arr[$Array1[$x]] = $Array2[$x];
}
}
输出是:
Array ( [bus] => 20 [int] => 10 )
您需要的代码就是这么简单:
// The input arrays
$Array1 = ['bus', 'bus', 'int'];
$Array2 = [2, 18, 10];
// Build the result here
$Array3 = [];
// There is no validation, the code assumes that $Array2 contains
// the same number of items as $Array1 or more
foreach ($Array1 as $index => $key) {
// If the key $key was not encountered yet then add it to the result
if (! array_key_exists($key, $Array3)) {
$Array3[$key] = 0;
}
// Add the value associate with $key to the sum in the results array
$Array3[$key] += $Array2[$index];
}
print_r($Array3);
它的输出:
Array
(
[bus] => 20
[int] => 10
)
这个可以工作:
$Array1 = ['bus', 'bus', 'int'];
$Array2 = [2, 18, 10];
# Let $Array3 be the required results array.
$Array3 = [];
for($j = 0; $j < count($Array1); $j += 1){
$k = $Array1[$j];
/*If the key already exists in $Array3 , add to it the value in the present key,
else just enter it as a new element. */
if(array_key_exists($k,$Array3)){ $Array3[$k] = $Array3[$k] + $Array2[$j];}
else {
# But first check that the length of $Array2 is not exceeded
if($j <= count($Array2)){
$Array3[$k] = $Array2[$j];
}
}
}
print_r($Array3);
# gives: Array ( [bus] => 20 [int] => 10 )