ammarr
ammarr

Reputation: 23

Multidimensional array alter key value pair

I have the following data array -

      0         1
#1   "Fname"  "Lname"
#2   "Tom"    "Shaw"
#3   "Marc"   "Hob"
#4   "Sim"    "Pow"

And I want to change it to a key value pair set where "Fname" will be the key for "Tom", "Mark" & "Sim" and "Lname" will be the key for "Shaw", "Hob" & "Pow.

If I loop through this array -

foreach ($data as $key=>$value) {
    echo $key.'<br>';
    foreach ($value as $v) {
        echo $v.'<br>';
    }
}

I get the following output -

0
Fname
Lname
1
Tom
Shaw
2
Marc
Hob
3
Sim
Pow

Which suggests that 'Fname, Lname', 'Tom, Shaw'.. etc. is another set of array, which I simply want as key value pair.

I am new to php programming and will really appreciate help from programming ninjas to solve this problem.

Thank you.

Upvotes: 2

Views: 94

Answers (4)

Maverick
Maverick

Reputation: 945

Is this what you want?

$arr = array(array('Fname','Lname'), array('Tom','Shaw'), array('Marc','Hob'), array('Sim','Pow'));

$out = array();

foreach($arr[0] as $cols){
        $out[$cols] = array();
}

foreach($arr as $key => $row){
    $i = '0';
    if($key != '0'){
        foreach($out as $out_key=>$out_row){
            $out[$out_key][] = $row[$i];
            $i++;
        }
    }
}

print_R($out);

OUTPUT

Array (
    [Fname] => Array
        (
            [0] => Tom
            [1] => Marc
            [2] => Sim
        )

    [Lname] => Array
        (
            [0] => Shaw
            [1] => Hob
            [2] => Pow
        )

)

Upvotes: 0

AbraCadaver
AbraCadaver

Reputation: 78994

Here's one way:

$keys = array_shift($data);
$result = array_map(function($v) use ($keys) { return array_combine($keys, $v); }, $data);

But might be simpler with a loop:

$keys = array_shift($data);
foreach($data as $vals) {
    $result[] = array_combine($keys, $vals);
}

Upvotes: 1

kittykittybangbang
kittykittybangbang

Reputation: 2400

I think @Marcovecchio was on the right track, but not quite spot on. Building on his code, you could use:

<?php

// create your new array
$new_array = array();

// pull 'Fname' and 'Lname' off original array, and set them as $col
$col = array_shift($data);

// iterate through original array, which now contains just names
foreach ($data as $value) {

    // set $col values as keys for each element
    $new_array[] = array($col[0] => $value[0], $col[1] => $value[1]);
}

// output what we just made
print_r($new_array);

?>

Output:

Array ( 
    [0] => Array ( 
        [Fname] => Tom 
        [Lname] => Shaw 
    ) 
    [1] => Array ( 
        [Fname] => Marc 
        [Lname] => Hob 
    ) 
    [2] => Array ( [
        Fname] => Sim 
        [Lname] => Pow 
    ) 
) 

Note: This assumes that the number of columns will always be 2.

Upvotes: 1

Marcovecchio
Marcovecchio

Reputation: 1332

This should do what you want:

$new_array = array();

foreach ($data as $value) 
{
  $new_array[] = array('Fname' => $value[0], 'Lname' => $value[1]);
}

Upvotes: 1

Related Questions