how convert the element structure of an array in php [duplicate]
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.
//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);
}
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
}
This will do:
$b = array();
foreach( $a as $sub1 ) {
foreach( $sub1 as $key => $value ) {
$b[ $key ][] = $value;
}
}
精彩评论