1

i have a m X n matrix

$a[0] [0] =" 4889-m.jpg";
$a[0] [1] ="  8675-m.jpg ";

$a[1] [0] ="  image/jpeg ";
$a[1] [1] ="  image/jpeg ";

$a[2] [0] ="C:\wamp\tmp\php9907.tmp ";
$a[2] [1] ="C:\wamp\tmp\php9908.tmp ";

$a[3] [0] ="";
$a[3] [1] ="";

$a[4] [0] =0;
$a[4] [1] =0;

$a[5] [0] = ;
$a[5] [1] =8005 ;

i want to convert it into n X m matrix

  $a[0][0=" 4889-m.jpg";
  $a[0][1]="  image/jpeg ";
  $a[0][2]="C:\wamp\tmp\php9907.tmp ";
  $a[0][3]="";
  $a[0][4]=0;
  $a[0][5]=13416;

  $a[1][0=" 8675-m.jpg ";
  $a[1][1]="  image/jpeg ";
  $a[1][2]="C:\wamp\tmp\php9908.tmp ";
  $a[1][3]="";
  $a[1][4]=0;
  $a[1][5]=13416;

also i have only $a its dimension is also unknown.

thank you for the help.

Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101

3 Answers3

4
//Get the count of the results in the array
$n = count($a);
//then for each item in the array loop
while($c = 0; $c =< $n; $c++){
   //get the number of each subitems in the array
   $nc = count($array[$c];
   //then again loop for each subitem in the array
   for($ni = 0; $ni =< $nc; $ni++){
       //then create a new array and set the items as nX
       $newarray[$ni][$nc] = $a[$ni];
   }
   //unset the $nc counter or it will be reset it self when it loops again
   unset($nc);

}
Vish
  • 4,508
  • 10
  • 42
  • 74
3

I think it is easiest to do it simply directly in PHP:

foreach ($a as $i => $row) 
    foreach ($row as $j => $val)
    {
        $b[$j][$i] = $val
    }
Tomas
  • 57,621
  • 49
  • 238
  • 373
2

This will do:

$b = array();
foreach( $a as $sub1 ) {
    foreach( $sub1 as $key => $value ) {
        $b[ $key ][] = $value;
    }
}
wonk0
  • 13,402
  • 1
  • 21
  • 15