0

I have a multidimensional array like this:

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
        )
)

I want to take only a few keys, for example 3 like this:

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

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

I have tried using array_column() but this can only take one key. is there the best way to do this?

2 Answers 2

1

Try below:-

$newarr = array();
foreach($arr as $key => $value){
    $newarr[] = array(
                  'NamaMk' => $value['NamaMk'], 
                  'JmlSks' => $value['JmlSks'], 
                  'JmlPresensiKuliah' => $value['JmlPresensiKuliah']
           );
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use array_map() function

Here is code

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.