Prasanth Jayakumar
Prasanth Jayakumar

Reputation: 9

How to convert Multidimensional array to Associative array in PhP?

I need to convert the multidimensional array to an associative array.

Please help me to convert the array. Need to remove the keys "sub_code" & "credits" and make the first string as "key" & the second string as "value". I tried a lot of ways but I failed.

Need to convert the below array.

array(9) { 
    [0]=> array(2) 
        {   
            ["sub_code"]    => string(6) "HS6151" 
            ["credits"]     => string(1) "4" 
        } 
    [1]=> array(2) 
        {   
            ["sub_code"]    => string(6) "MA6151" 
            ["credits"]     => string(1) "4" 
        } 
    [2]=> array(2) 
        {   
            ["sub_code"]    => string(6) "PH6151" 
            ["credits"]     => string(1) "3" 
        } 
    [3]=> array(2) 
        {   
            ["sub_code"]    => string(6) "CY6151" 
            ["credits"]     => string(1) "3" 
        } 
    [4]=> array(2) 
        {   
            ["sub_code"]    => string(6) "GE6151" 
            ["credits"]     => string(1) "3" 
        } 
    [5]=> array(2) 
        {   
            ["sub_code"]    => string(6) "GE6152" 
            ["credits"]     => string(1) "4" 
        } 
    [6]=> array(2) 
        {   
            ["sub_code"]    => string(6) "GE6161" 
            ["credits"]     => string(1) "2" 
        } 
    [7]=> array(2) 
        {   
            ["sub_code"]    => string(6) "GE6162" 
            ["credits"]     => string(1) "2" 
        } 
    [8]=> array(2) 
        {   
            ["sub_code"]    => string(6) "GE6163" 
            ["credits"]     => string(1) "1" 
        } 
}

Like below.

array(9) { 
    ["HS6151"]  =>  string(1) "4" 
    ["MA6151"]  =>  string(1) "4" 
    ["PH6151"]  =>  string(1) "3" 
    ["CY6151"]  =>  string(1) "3" 
    ["GE6151"]  =>  string(1) "3" 
    ["GE6152"]  =>  string(1) "4" 
    ["GE6161"]  =>  string(1) "2" 
    ["GE6162"]  =>  string(1) "2" 
    ["GE6163"]  =>  string(1) "1" 
}

Upvotes: 1

Views: 2661

Answers (2)

arkascha
arkascha

Reputation: 42915

Sounds pretty straight forward:

<?php
$input = [
    [
        "sub_code" => "HS6151", 
        "credits"  => "4" 
    ], [
        "sub_code" => "MA6151",
        "credits"  => "4" 
    ], [
        "sub_code" => "PH6151",
        "credits"  => "3" 
    ], [
        "sub_code" => "CY6151", 
        "credits"  => "3" 
    ], [
        "sub_code" => "GE6151", 
        "credits"  => "3" 
    ], [
        "sub_code" => "GE6152", 
        "credits"  => "4" 
    ], [
        "sub_code" => "GE6161", 
        "credits"  => "2" 
    ], [
        "sub_code" => "GE6162", 
        "credits"  => "2" 
    ], [
        "sub_code" => "GE6163", 
        "credits"  => "1" 
    ]
];

$output = [];
array_walk($input, function($entry) use (&$output) {
    $output[$entry["sub_code"]] = $entry["credits"];
});

print_r($output);

The output obviously is:

Array
(
    [HS6151] => 4
    [MA6151] => 4
    [PH6151] => 3
    [CY6151] => 3
    [GE6151] => 3
    [GE6152] => 4
    [GE6161] => 2
    [GE6162] => 2
    [GE6163] => 1
)

Upvotes: 0

Mark
Mark

Reputation: 179

You can use array_column like so:

$array = [
    [
        'sub_code' => 'HS6151',
        'credits' => '4',
    ],
    [
        'sub_code' => 'MA6151',
        'credits' => '4',
    ],
];

$result = array_column($array, 'credits', 'sub_code');
print_r($result);

Results in:

Array
(
    [HS6151] => 4
    [MA6151] => 4
)

Upvotes: 4

Related Questions