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

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"}]


/*
 * 好吧。不知道是不是你需要的。还有想说一句,有这个必要吗?
 * */
$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 '<br>';

print_r(array_filter($b,'filtertypetwo'));

function filtertype($var)
{
    return $var['type'] ==1;
}

function filtertypetwo($var)
{
    return $var['type'] ==2;
}

array_map(function($v){
    if($v['type'] == 1){
        $GLOBALS['new'][] = $v;
    }else if($v['type'] == 2){
        $GLOBALS['old'][] = $v;
    } 
}, json_decode($json, true));

array_column — 返回数组中指定的一列 (注意版本要求。PHP 5 >= 5.5.0)

<?php
 $records  = array(
    array(
         'id'  =>  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
)

除了不循环,我想不到别的法子,关注一下,等高人回答。


$arr = [];//给的数组
$result = array_map(function($v){
    if($v['type'] == 1) return $v;
},$arr);
var_dump($result);
【热门文章】
【热门文章】