Ravinder Singh
Ravinder Singh

Reputation: 3133

Remove the first row from a 2d array and use those values as keys for each subsequent row

I am having this array :

array(
    0 => array("name", "address", "city"), 
    1=> array( "anoop", "palasis", "Indore"),
    2=> array( "ravinder", "annapurna", "Indore")
)

and I want to make this array in this way :

array( 
    0 => array("name" = >"anoop" , "address" = >"palasia", "city" = >"Indore"),
    1 => array("name" = >"ravinder" , "address" = >"annapurna", "city" = >"Indore")
)

Upvotes: 4

Views: 17902

Answers (5)

mickmackusa
mickmackusa

Reputation: 48073

Consume the first row and use it as the keys for all subsequent rows: Demo

array_walk(
    $array,
    fn(&$row, $_, $header) => $row = array_combine($header, $row),
    array_shift($array)
);
var_export($array);

Using array_map() is even simpler and avoids mutating the original input array. Demo

var_export(
    array_map(
        fn($row) => array_combine($array[0], $row),
        array_slice($array, 1)
    )
);

Upvotes: 0

Mycelin
Mycelin

Reputation: 647

The modern way is:

$data = array_column($data, 'value', 'key');

In your case:

$data = array_column($data, 1, 0);

Upvotes: 10

wecsam
wecsam

Reputation: 2771

Here is a function that you can use:

function rewrap(Array $input){
    $key_names = array_shift($input);
    $output = Array();
    foreach($input as $index => $inner_array){
        $output[] = array_combine($key_names,$inner_array);
    }
    return $output;
}

Here is a demonstration:

// Include the function from above here 
$start = array(
    0 => array("name", "address", "city"),
    1 => array("anoop", "palasis", "Indore"),
    2 => array("ravinder", "annapurna", "Indore")
);
print_r(rewrap($start));

This outputs:

Array
(
    [0] => Array
        (
            [name] => anoop
            [address] => palasis
            [city] => Indore
        )

    [1] => Array
        (
            [name] => ravinder
            [address] => annapurna
            [city] => Indore
        )

)

Note: Your first array defined index 1 twice, so I changed the second one to 2, like this:

array(0 => array("name", "address", "city"), 1 => array("anoop", "palasis", "Indore"),2 => array("ravinder", "annapurna", "Indore"))

That was probably just a typo.

Upvotes: 1

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57690

Use array_combine. If $array contains your data

$result = array(
             array_combine($array[0], $array[1]),
             array_combine($array[0], $array[2])
          );

In general

$result = array();
$len = count($array);
for($i=1;$i<$len; $i++){
    $result[] = array_combine($array[0], $array[$i]);
}

Upvotes: 4

Borealid
Borealid

Reputation: 98559

If your data are in $array:

$res = array();
foreach ($array as $key=>$value) {
    if ($key == 0) {
        continue;
    }
    for ($i = 0; $i < count($array[0]); $i++) {
        $res[$array[0][$i]] = $value[$i];
    }
}

The result is now in $res.

Upvotes: 2

Related Questions