是一個二維數組[別糾結,為了方便我將其轉為了json], 其中type只有兩個值1和2,
現在需要將type=1的元素轉到$new數組中,type=2的轉到$old數組中。
除了用迴圈還有什麼方法沒?
附上json:
[{"id":"10","type":"1","cpu":"Intel Xeon E3-1241v3","cores":"4","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"148","price1":"142","price2":"135","price3":"127","price4":"118","add_time":"0000-00-00 00:00:00","edit_time":"0000-00-00 00:00:00","ip":null},{"id":"11","type":"1","cpu":"Intel Xeon E5-1650v2","cores":"6","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"198","price1":"190","price2":"180","price3":"170","price4":"158","add_time":"0000-00-00 00:00:00","edit_time":"0000-00-00 00:00:00","ip":null},{"id":"12","type":"1","cpu":"Dual Intel Xeon E5-2630v2","cores":"12","ram":"64","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"348","price1":"334","price2":"317","price3":"299","price4":"278","add_time":"0000-00-00 00:00:00","edit_time":"0000-00-00 00:00:00","ip":null},{"id":"4","type":"2","cpu":"Intel Core i3-2100T","cores":"2","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"68","price1":"65","price2":"61","price3":"56","price4":"51","add_time":"2015-09-16 16:36:39","edit_time":"2015-09-16 16:36:42","ip":"127.0.0.1"},{"id":"5","type":"2","cpu":"Intel Xeon X3470","cores":"4","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"78","price1":"75","price2":"70","price3":"65","price4":"59","add_time":"2015-09-16 16:37:24","edit_time":"2015-09-16 16:37:27","ip":"127.0.0.1"},{"id":"6","type":"2","cpu":"Intel Core i5-2400","cores":"4","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"88","price1":"84","price2":"79","price3":"73","price4":"66","add_time":"2015-09-16 16:38:14","edit_time":"2015-09-16 16:38:16","ip":"127.0.0.1"},{"id":"7","type":"2","cpu":"Intel Core i7-2600","cores":"4","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"98","price1":"94","price2":"88","price3":"81","price4":"74","add_time":"2015-09-16 16:40:02","edit_time":"2015-09-16 16:40:04","ip":"127.0.0.1"},{"id":"8","type":"2","cpu":"Intel Core i7-3770S","cores":"4","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"118","price1":"113","price2":"106","price3":"98","price4":"89","add_time":"2015-09-16 16:40:50","edit_time":"2015-09-16 16:40:52","ip":"127.0.0.1"},{"id":"9","type":"2","cpu":"Dual Intel Xeon X5650","cores":"12","ram":"64","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"318","price1":"305","price2":"286","price3":"264","price4":"239","add_time":"2015-09-16 16:42:00","edit_time":"2015-09-16 16:42:02","ip":"127.0.0.1"}]
回複內容:
是一個二維數組[別糾結,為了方便我將其轉為了json], 其中type只有兩個值1和2,
現在需要將type=1的元素轉到$new數組中,type=2的轉到$old數組中。
除了用迴圈還有什麼方法沒?
附上json:
[{"id":"10","type":"1","cpu":"Intel Xeon E3-1241v3","cores":"4","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"148","price1":"142","price2":"135","price3":"127","price4":"118","add_time":"0000-00-00 00:00:00","edit_time":"0000-00-00 00:00:00","ip":null},{"id":"11","type":"1","cpu":"Intel Xeon E5-1650v2","cores":"6","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"198","price1":"190","price2":"180","price3":"170","price4":"158","add_time":"0000-00-00 00:00:00","edit_time":"0000-00-00 00:00:00","ip":null},{"id":"12","type":"1","cpu":"Dual Intel Xeon E5-2630v2","cores":"12","ram":"64","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"348","price1":"334","price2":"317","price3":"299","price4":"278","add_time":"0000-00-00 00:00:00","edit_time":"0000-00-00 00:00:00","ip":null},{"id":"4","type":"2","cpu":"Intel Core i3-2100T","cores":"2","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"68","price1":"65","price2":"61","price3":"56","price4":"51","add_time":"2015-09-16 16:36:39","edit_time":"2015-09-16 16:36:42","ip":"127.0.0.1"},{"id":"5","type":"2","cpu":"Intel Xeon X3470","cores":"4","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"78","price1":"75","price2":"70","price3":"65","price4":"59","add_time":"2015-09-16 16:37:24","edit_time":"2015-09-16 16:37:27","ip":"127.0.0.1"},{"id":"6","type":"2","cpu":"Intel Core i5-2400","cores":"4","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"88","price1":"84","price2":"79","price3":"73","price4":"66","add_time":"2015-09-16 16:38:14","edit_time":"2015-09-16 16:38:16","ip":"127.0.0.1"},{"id":"7","type":"2","cpu":"Intel Core i7-2600","cores":"4","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"98","price1":"94","price2":"88","price3":"81","price4":"74","add_time":"2015-09-16 16:40:02","edit_time":"2015-09-16 16:40:04","ip":"127.0.0.1"},{"id":"8","type":"2","cpu":"Intel Core i7-3770S","cores":"4","ram":"16","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"118","price1":"113","price2":"106","price3":"98","price4":"89","add_time":"2015-09-16 16:40:50","edit_time":"2015-09-16 16:40:52","ip":"127.0.0.1"},{"id":"9","type":"2","cpu":"Dual Intel Xeon X5650","cores":"12","ram":"64","hard_drive":"1TB SATA","port_speed":"100Mbps","transfer":"20TB","ipmi_kvm":"Y","price":"318","price1":"305","price2":"286","price3":"264","price4":"239","add_time":"2015-09-16 16:42:00","edit_time":"2015-09-16 16:42:02","ip":"127.0.0.1"}]
/* * 好吧。不知道是不是你需要的。還有想說一句,有這個必要嗎? * */$b = array( array('type' => 1, 'name' => 'lisi'), array('type' => 2, 'name' => 'lisi'), array('type' => 1, 'name' => 'lisi'), array('type' => 1, 'name' => 'lisi'), );$newArray = array();print_r(array_filter($b,'filtertype'));echo '
';print_r(array_filter($b,'filtertypetwo'));function filtertype($var){ return $var['type'] ==1;}function filtertypetwo($var){ return $var['type'] ==2;}
array_column — 返回數組中指定的一列 (注意版本要求。PHP 5 >= 5.5.0)
2135 , 'first_name' => 'John' , 'last_name' => 'Doe' , ), array( 'id' => 3245 , 'first_name' => 'Sally' , 'last_name' => 'Smith' , ), array( 'id' => 5342 , 'first_name' => 'Jane' , 'last_name' => 'Jones' , ), array( 'id' => 5623 , 'first_name' => 'Peter' , 'last_name' => 'Doe' , )); $first_names = array_column ( $records , 'first_name' ); print_r ( $first_names ); ?> 以上常式會輸出:Array( [0] => John [1] => Sally [2] => Jane [3] => Peter)
除了不迴圈,我想不到別的法子,關注一下,等高人回答。
array_map(function($v){ if($v['type'] == 1){ $GLOBALS['new'][] = $v; }else if($v['type'] == 2){ $GLOBALS['old'][] = $v; } }, json_decode($json, true));
$arr = [];//給的數組$result = array_map(function($v){ if($v['type'] == 1) return $v;},$arr);var_dump($result);