How does PHP convert a two-dimensional array with the same key name to a new array? No For,foreach loops

Source: Internet
Author: User
Tags xeon e5 intel core i7 intel core i5

is a two-dimensional array [don't tangle, in order to make it easier for me to convert it to JSON], where type has only two values of 1 and 2,
Now you need to move the type=1 element into the $new array, type=2 to the $old array.

Is there any way besides using the loop?

Attach JSON:

[{"id": "Ten", "Type": "1", "CPU": "Intel Xeon e3-1241v3", "Cores": "4", "Ram": "+", "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": "One", "type": "1", "CPU": "Intel Xeon e5-1650v2", "Cores": "6", "Ram": "+", "hard_drive": "1TB SATA", "Port_speed": "100Mbps", "Transfer": "20TB", " IPMI_KVM ":" Y "," Price ":" 198 "," Price1 ":" Price3 "," Price2 ":" The "," Price4 ":" "," 158 ":" "," Add_time ":" 0000-00-00 00:00:00 "," Edit_time ":" 0000-00-00 00:00:00 "," IP ": null},{" id ":" "", "Type": "1", "CPU": "Dual Intel Xeon e5-2630v2", " Cores ":" "", "Ram": "100Mbps", "hard_drive": "1TB SATA", "Port_speed": "20TB", "Transfer": "X", "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": "+", "haRd_drive ":" 1TB SATA "," Port_speed ":" 100Mbps "," Transfer ":" 20TB "," IPMI_KVM ":" Y "," Price ":" "," Price1 ":" X "," Price2 ":", "Price3": "Price4", "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 ":" + "," hard_drive ":" 1TB SATA "," Port_ Speed ":" 100Mbps "," Transfer ":" 20TB "," IPMI_KVM ":" Y "," Price ":" Price1 "," Price2 ":" X "," Price3 ":" 65 "," Price4 ":", "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 ":" + "," hard_drive ":" 1TB SATA "," Port_speed ":" 100Mbps "," Transfer ":" 20TB "," IPMI_KVM ":" Y "," Price ":" X "," Price1 ":" X "," Price2 ":" X "," Price3 ":" X "," Price4 ":" X "," 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 ":" + "," hard_drive ":" 1TB SATA "," Port_speed ":" 100Mbps "," Transfer ":" 20TB "," IPMI_KVM " : "Y", "Price": "98 "," Price1 ":" 94 "," Price2 ":" The "," Price3 ":" Bayi "," Price4 ":" "," 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 ":" The "," 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 ":" 1TB "," Ram ":" + "," hard_drive " "Port_speed": "100Mbps", "Transfer": "20TB", "IPMI_KVM": "Y", "Price": "318", "Price1": "305", "Price2": "286", "Price3": " "," "Price4": "239", "Add_time": "2015-09-16 16:42:00", "Edit_time": "2015-09-16 16:42:02", "IP": "127.0.0.1"}]

Reply content:

is a two-dimensional array [don't tangle, in order to make it easier for me to convert it to JSON], where type has only two values of 1 and 2,
Now you need to move the type=1 element into the $new array, type=2 to the $old array.

Is there any way besides using the loop?

Attach JSON:

[{"id": "Ten", "Type": "1", "CPU": "Intel Xeon e3-1241v3", "Cores": "4", "Ram": "+", "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": "One", "type": "1", "CPU": "Intel Xeon e5-1650v2", "Cores": "6", "Ram": "+", "hard_drive": "1TB SATA", "Port_speed": "100Mbps", "Transfer": "20TB", " IPMI_KVM ":" Y "," Price ":" 198 "," Price1 ":" Price3 "," Price2 ":" The "," Price4 ":" "," 158 ":" "," Add_time ":" 0000-00-00 00:00:00 "," Edit_time ":" 0000-00-00 00:00:00 "," IP ": null},{" id ":" "", "Type": "1", "CPU": "Dual Intel Xeon e5-2630v2", " Cores ":" "", "Ram": "100Mbps", "hard_drive": "1TB SATA", "Port_speed": "20TB", "Transfer": "X", "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": "+", "haRd_drive ":" 1TB SATA "," Port_speed ":" 100Mbps "," Transfer ":" 20TB "," IPMI_KVM ":" Y "," Price ":" "," Price1 ":" X "," Price2 ":", "Price3": "Price4", "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 ":" + "," hard_drive ":" 1TB SATA "," Port_ Speed ":" 100Mbps "," Transfer ":" 20TB "," IPMI_KVM ":" Y "," Price ":" Price1 "," Price2 ":" X "," Price3 ":" 65 "," Price4 ":", "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 ":" + "," hard_drive ":" 1TB SATA "," Port_speed ":" 100Mbps "," Transfer ":" 20TB "," IPMI_KVM ":" Y "," Price ":" X "," Price1 ":" X "," Price2 ":" X "," Price3 ":" X "," Price4 ":" X "," 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 ":" + "," hard_drive ":" 1TB SATA "," Port_speed ":" 100Mbps "," Transfer ":" 20TB "," IPMI_KVM " : "Y", "Price": "98 "," Price1 ":" 94 "," Price2 ":" The "," Price3 ":" Bayi "," Price4 ":" "," 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 ":" The "," 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 ":" 1TB "," Ram ":" + "," hard_drive " "Port_speed": "100Mbps", "Transfer": "20TB", "IPMI_KVM": "Y", "Price": "318", "Price1": "305", "Price2": "286", "Price3": " "," "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-returns the specified column in the array ( note the version requirement .) 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)

In addition to not cycle, I think there is no other way, pay attention to, such as the Tall Man answer.

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);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.