I have 3 arrays . I need to combine them using the Names as key, remove the duplicates (names duplicates ); only the most valuable must remain (high value based on the sales array). I'm wondering if there is any easy way to do this. All I have in my mind is several foreach operations which are boring.
我有3个阵列。我需要使用Names作为键来组合它们,删除重复项(名称重复);只有最有价值的东西必须保留(基于销售数组的高价值)。我想知道是否有任何简单的方法来做到这一点。我所有的想法都是几个令人厌烦的foreach操作。
[Names] => Array ( [0] => Adrew Watson [1] => Maria Jones [2] => Adrew Watson )[sales] => Array ( [0] => 12,000.00 [1] => 11,900.00 [2] => 11,800.00 ) [time] => Array ( [0] => 31-Jan-13 [1] => 30-Jan-13 [2] => 29-Jan-13 )
The output should be something like :
输出应该是这样的:
[Adrew Watson] => sales => 12,000.00 time => 31-Jan-13 [Maria Jones] => sales => 11,900.00 time => 30-Jan-13
I think it would be an associative array with the duplicates removed by "value" . Please note that the values of each array (e.g. sales ) is already ordered DESC
我认为这将是一个关联数组,重复项被“值”删除。请注意,每个数组(例如销售)的值已经订购了DESC
2 个解决方案
#1
3
// Assumed from your output the arrays have the indexes Names, sales and time
$names = array('Names' => array('Adrew Watson','Maria Jones','Adrew Watson'));
$sales = array('sales' => array('12,000.00','11,900.00','11,800.00'));
$time = array('time' => array('31-Jan-13','30-Jan-13','29-Jan-13'));
$new = array();
foreach ($names['Names'] as $key => $value)
{
if (isset($new[$value])) continue; // if already exists, skip
$new[$value] = array(
'sales' => $sales['sales'][$key],
'time' => $time['time'][$key]);
}
print_r($new);
Outputs:
Array
(
[Adrew Watson] => Array
(
[sales] => 12,000.00
[time] => 31-Jan-13
)
[Maria Jones] => Array
(
[sales] => 11,900.00
[time] => 30-Jan-13
)
)
As per your below comment:
根据您的以下评论:
If you want to remove a Name
from the array you can do so AFTER running the above code by doing this for example:
如果要从数组中删除名称,可以在运行上述代码后执行此操作,例如:
unset($new['Maria Jones']); // remove Maria Jones
#2
1
this can be done by array_merge than array_unique
这可以通过array_merge而不是array_unique来完成
<?php
$array1 = array("orange", "apple", "grape");
$array2 = array("peach", "plumb","apple");
$array3 = array("lemon", "plumb");
$newArray = array_merge($array1, $array2, $array3);
$result = array_unique($newArray);
print_r($result);
#1
3
// Assumed from your output the arrays have the indexes Names, sales and time
$names = array('Names' => array('Adrew Watson','Maria Jones','Adrew Watson'));
$sales = array('sales' => array('12,000.00','11,900.00','11,800.00'));
$time = array('time' => array('31-Jan-13','30-Jan-13','29-Jan-13'));
$new = array();
foreach ($names['Names'] as $key => $value)
{
if (isset($new[$value])) continue; // if already exists, skip
$new[$value] = array(
'sales' => $sales['sales'][$key],
'time' => $time['time'][$key]);
}
print_r($new);
Outputs:
Array
(
[Adrew Watson] => Array
(
[sales] => 12,000.00
[time] => 31-Jan-13
)
[Maria Jones] => Array
(
[sales] => 11,900.00
[time] => 30-Jan-13
)
)
As per your below comment:
根据您的以下评论:
If you want to remove a Name
from the array you can do so AFTER running the above code by doing this for example:
如果要从数组中删除名称,可以在运行上述代码后执行此操作,例如:
unset($new['Maria Jones']); // remove Maria Jones
#2
1
this can be done by array_merge than array_unique
这可以通过array_merge而不是array_unique来完成
<?php
$array1 = array("orange", "apple", "grape");
$array2 = array("peach", "plumb","apple");
$array3 = array("lemon", "plumb");
$newArray = array_merge($array1, $array2, $array3);
$result = array_unique($newArray);
print_r($result);