如何从两个阵列获得独特的月份? 我有两个数组:
$ar1 =Array
(
[0] => Array
(
[0] => 1
[month] => 1
[1] => -40964.49999999999
[total] => -40964.49999999999
)
[1] => Array
(
[0] => 2
[month] => 2
[1] => -41353.93
[total] => -41353.93
)
[2] => Array
(
[0] => 3
[month] => 3
[1] => -32716.35
[total] => -32716.35
)
[3] => Array
(
[0] => 4
[month] => 4
[1] => -30399.059999999998
[total] => -30399.059999999998
)
[4] => Array
(
[0] => 5
[month] => 5
[1] => -33781.689999999995
[total] => -33781.689999999995
)
[5] => Array
(
[0] => 6
[month] => 6
[1] => -26129.219999999994
[total] => -26129.219999999994
)
[6] => Array
(
[0] => 7
[month] => 7
[1] => -40643.990000000005
[total] => -40643.990000000005
)
[7] => Array
(
[0] => 8
[month] => 8
[1] => -63260.61
[total] => -63260.61
)
)
$ar2 = Array
(
[0] => Array
(
[0] => 1
[month] => 1
[1] => 40616.1
[total] => 40616.1
)
[1] => Array
(
[0] => 2
[month] => 2
[1] => 39640.659999999996
[total] => 39640.659999999996
)
[2] => Array
(
[0] => 3
[month] => 3
[1] => 34639.73
[total] => 34639.73
)
[3] => Array
(
[0] => 4
[month] => 4
[1] => 37337.62
[total] => 37337.62
)
[4] => Array
(
[0] => 5
[month] => 5
[1] => 35776.98000000001
[total] => 35776.98000000001
)
[5] => Array
(
[0] => 6
[month] => 6
[1] => 35001.97
[total] => 35001.97
)
[6] => Array
(
[0] => 7
[month] => 7
[1] => 32456.749999999996
[total] => 32456.749999999996
)
[7] => Array
(
[0] => 8
[month] => 8
[1] => 41992.56999999999
[total] => 41992.56999999999
)
[8] => Array
(
[0] => 9
[month] => 9
[1] => 238.18
[total] => 238.18
)
[9] => Array
(
[0] => 10
[month] => 10
[1] => 238.22
[total] => 238.22
)
)
如何从两个阵列获得独特的月份?
答案 0 :(得分:1)
在将两个数组的所有月份存储到单个数组后,您可以尝试使用函数array_unique():
$arr_tmp = [];
$res = [];
foreach($ar1 as $a1) {
$arr_tmp[] = $a1['month'];
}
foreach($ar2 as $a2 {
$arr_tmp[] = $a2['month'];
}
$res = array_unique($arr_tmp);
答案 1 :(得分:0)
使用array_diff,但由于你的数组是多维的,所以你需要在array_udiff中提供比较函数。
请参阅下面的答案以获取示例