您好,欢迎访问一九零五行业门户网

php怎么将二维数组键名相同的元素转为新的数组?不用for,foreach循环

如图所示是一个二维数组[别纠结,为了方便我将其转为了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);
其它类似信息

推荐信息