在多维php数组中采用几个值

时间:2019-03-29 19:17:36

标签: php arrays multidimensional-array

我有一个像这样的多维数组:

Array
(
    [0] => Array
        (
            [KrsId] => 2451493
            [Kode] => ST050
            [NamaMk] => MANAJEMEN STRATEGIK
            [NamaMkEn] => STRATEGIC MANAGEMENT
            [JmlSks] => 2
            [JmlPresensiKuliah] => 3
            [IsHadirMID] => 0
            [IsHadirUAS] => 0
        )

    [1] => Array
        (
            [KrsId] => 2451488
            [Kode] => ST087
            [NamaMk] => MANAJEMEN SUMBERDAYA IT
            [NamaMkEn] => IT RESOURCES MANAGEMENT
            [JmlSks] => 2
            [JmlPresensiKuliah] => 3
            [IsHadirMID] => 0
            [IsHadirUAS] => 0
        )
)

我只想使用几个键,例如3个这样的

Array
(
    [0] => Array
        (
            [NamaMk] => MANAJEMEN STRATEGIK
            [JmlSks] => 2
            [JmlPresensiKuliah] => 3
        )

    [1] => Array
        (
            [NamaMk] => MANAJEMEN SUMBERDAYA IT
            [JmlSks] => 2
            [JmlPresensiKuliah] => 3
        )
)

我尝试使用array_column(),但是只能使用一个键。有最好的方法吗?

2 个答案:

答案 0 :(得分:1)

尝试以下:-

$newarr = array();
foreach($arr as $key => $value){
    $newarr[] = array(
                  'NamaMk' => $value['NamaMk'], 
                  'JmlSks' => $value['JmlSks'], 
                  'JmlPresensiKuliah' => $value['JmlPresensiKuliah']
           );
}

答案 1 :(得分:1)

您可以使用array_map()功能

这是代码

   $newArr = array_map(function($value){
        return [ "NamaMk" =>$value['NamaMk'],
                 "JmlSks" => $value['JmlSks'],
                 "JmlPresensiKuliah" => $value['JmlPresensiKuliah']
               ];
    },$yourArr);