kamal
kamal

Reputation: 1546

Array 1st value as a key on new array and second as its values

I have a array:

 $array1 =  array
      0 => 
        array
          0 => string 'biodata' (length=7)
          1 => string 'family_name' (length=11)
      1 => 
        array
          0 => string 'biodata' (length=7)
          1 => string 'first_name' (length=10)
      2 => 
        array
          0 => string 'biodata_education' (length=17)
          1 => string 'subject' (length=7)
      3 => 
        array
          0 => string 'biodata_education' (length=20)
          1 => string 'year' (length=5)

which need to converted like:

array
  biodata => 
    array
      0 => string 'family_name' (length=7)
      1 => string 'first_name' (length=11)
  biodata_education => 
    array
      0 => string 'subject' (length=7)
      1 => string 'year' (length=10)

as it can be done by simple iteration, I tried this one and done.

foreach($array1 as $tbl):
            $table[$tbl[0]][] = $tbl[1];

        endforeach;

Upvotes: 0

Views: 100

Answers (1)

kamal
kamal

Reputation: 1546

<?php

//map the array using a foreach loop    
foreach($array1 as $tbl)
{
    $table[ $tbl[0] ][] = $tbl[1];
}

Upvotes: 1

Related Questions