3

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")
)
Ravinder Singh
  • 3,113
  • 6
  • 30
  • 46

5 Answers5

8

The modern way is:

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

In your case:

$data = array_column($data, 1, 0);
Mycelin
  • 607
  • 8
  • 6
4

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]);
}
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
2

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.

Borealid
  • 95,191
  • 9
  • 106
  • 122
  • -1 for executing `count()` for every iteration of not only the inner `for` loop, but also the outer `foreach` loop. –  Feb 18 '12 at 18:13
  • @Phoenix Does efficiency even matter here at all? Are you serious? – Borealid Feb 18 '12 at 18:14
  • 1
    It's a bad practice. The array that is presented in the OP has 2 rows to process, but the array may have been truncated for posting here; it might actually hold thousands of records. Or someone might come across this answer while solving a similar problem and pick up a bad habit. –  Feb 18 '12 at 18:17
  • 2
    @Phoenix No, it's counting the length of the *first row*. Which is like five elements long, and is never going to be "thousands". Premature optimization is the root of all evil. – Borealid Feb 18 '12 at 18:21
  • 1
    The `count` in `for` loop is so well known bad practice that it tempts to down vote as soon as you see it. I down voted. Then canceled. :( – Shiplu Mokaddim Feb 18 '12 at 18:22
  • @Borealid That's a fair point. However, I still stand by my -1. Outside of very unusual circumstances, there is no good reason to do it that way. –  Feb 18 '12 at 18:24
1

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.

wecsam
  • 2,651
  • 4
  • 25
  • 46
0

Assuming that you are parsing a CSV file, check out the answers to this question:

Get associative array from csv

Community
  • 1
  • 1
The Nail
  • 8,355
  • 2
  • 35
  • 48
  • Either close vote or comment. It can not be answer.[should-we-flag-answers-that-only-contain-a-link-to-another-so-answer](http://meta.stackexchange.com/questions/95491/should-we-flag-answers-that-only-contain-a-link-to-another-so-answer). – Shiplu Mokaddim Feb 18 '12 at 18:20
  • Thanks, I have read the discussion. However, I do not agree, and think there is some added value in linking to a question that refers to the concept of an *associative array*, not mentioned before by the OP. – The Nail Feb 18 '12 at 18:32