這裡,我要實現的是兩個二維陣列,根據某乙個字段相同,將其他的資料相加
陣列一:
$data1 = array(
0=>array( "days"=>"2018-10-11","ios"=>"3", "android"=>"92"),
1=>array( "days"=>"2018-10-10", "ios"=>"103" ,"android"=>"569" ),
2=>array( "days"=>"2018-10-09" ,"ios"=>"234", "android"=>"568" ) ,
3=>array( "days"=> "2018-10-08" ,"ios"=> "222" ,"android"=> "548" ) ,
4=>array( "days"=> "2018-10-07" ,"ios"=> "230" ,"android"=> "736" ) ,
5=>array( "days"=> "2018-10-06" ,"ios"=> "656", "android"=> "885" ) ,
6=>array( "days"=> "2018-10-05" ,"ios"=> "333" ,"android"=> "894" ) ,
);
陣列二:
$data2 = array(
0=>array("days"=> "2018-10-10","ios"=> "1","android"=> "2") ,
1=>array("days"=> "2018-10-09" ,"ios"=> "5" ,"android"=> "5" ) ,
2=>array("days"=> "2018-10-08" ,"ios"=> "2" ,"android"=> "4" ) ,
3=>array("days"=> "2018-10-07" ,"ios"=> "2" ,"android"=> "10" ) ,
4=>array("days"=> "2018-10-06" ,"ios"=> "8" ,"android"=> "14" ) ,
5=>array("days"=> "2018-10-05" ,"ios"=> "2" ,"android"=> "16" ) ,
);
根據days相同將ios和android分別相加
我們先將兩個陣列合併:
$data = array_merge($data1,$data2);
結果:
array
( [0] => array
([days] => 2018-10-11
[ios] => 3
[android] => 92
)[1] => array
([days] => 2018-10-10
[ios] => 103
[android] => 569
)[2] => array
([days] => 2018-10-09
[ios] => 234
[android] => 568
)[3] => array
([days] => 2018-10-08
[ios] => 222
[android] => 548
)[4] => array
([days] => 2018-10-07
[ios] => 230
[android] => 736
)[5] => array
([days] => 2018-10-06
[ios] => 656
[android] => 885
)[6] => array
([days] => 2018-10-05
[ios] => 333
[android] => 894
)[7] => array
([days] => 2018-10-10
[ios] => 1
[android] => 2
)[8] => array
([days] => 2018-10-09
[ios] => 5
[android] => 5
)[9] => array
([days] => 2018-10-08
[ios] => 2
[android] => 4
)[10] => array
([days] => 2018-10-07
[ios] => 2
[android] => 10
)[11] => array
([days] => 2018-10-06
[ios] => 8
[android] => 14
)[12] => array
([days] => 2018-10-05
[ios] => 2
[android] => 16
))
再對這個合併後的陣列進行處理
$newdata = ;
foreach($data as $k=>$v)else
}
結果:列印 p(array_values($newdata));
array
( [0] => array
([days] => 2018-10-11
[ios] => 3
[android] => 92
)[1] => array
([days] => 2018-10-10
[ios] => 104
[android] => 571
)[2] => array
([days] => 2018-10-09
[ios] => 239
[android] => 573
)[3] => array
([days] => 2018-10-08
[ios] => 224
[android] => 552
)[4] => array
([days] => 2018-10-07
[ios] => 232
[android] => 746
)[5] => array
([days] => 2018-10-06
[ios] => 664
[android] => 899
)[6] => array
([days] => 2018-10-05
[ios] => 335
[android] => 910
))
這個就是我們想要的陣列 PHP 二維陣列根據某個字段排序
php view plain copy 二維陣列根據某個字段排序 功能 按照使用者的年齡倒序排序 author ruxing.li header content type text html charset utf 8 arrusers array array id 1,name 張三 age 25...
PHP二維陣列根據某個字段排序
二維陣列根據某個字段排序 功能 按照使用者的年齡倒序排序 author ruxing.li header content type text html charset utf 8 arrusers array array id 1,name 張三 age 25,array id 2,name 李四 ...
PHP 二維陣列根據某個字段排序
本文記錄的要實現的功能類似於 mysql 中的 order by,上個專案中有遇到這樣的乙個需求。要求 從兩個不同的表中獲取各自的4條資料,然後整合 array merge 成乙個陣列,再根據資料的建立時間降序排序取前4條。遇到這個要求的時候就不是 order by 能解決的問題了。因此翻看 php...