I have the following PHP array:
$b = array
(
0 =>"03xxx", //Index of main, substr(index,0,2)
1 =>"04xxx",
2 =>"05xxx",
//3 =>"06xxx" // missing "06"
//4 =>"07xxx",
6 =>"04xxx",
7 =>"05xxx",
8 =>"06xxx",
//9 =>"07xxx"
10 =>"08xxx",
11 =>"03xxx" ,// new index of main
12 =>"04xxx",
13 =>"05xxx",
14 =>"06xxx",
//15 =>"07xxx" missing "07"
16 =>"08xxx"
);
I need to transform it to:
/*the expected result
Array
(
[0] => Array
(
[0] => 03xxx04xxx05xxx 08xxx, // missing 06 ,07
[1] => 03xxx04xxx05xxx06xxx 08xxx // missing 07
)
[1] => Array
(
[0] => 03xxx04xxx05xxx06xxx 08xxx // missing 07
)
)
*/
Update:
Based on yes123’s answer I arrived at the following solution:
/*
* thank to @yes123
*/
function transform($a){
$result=array();
$i=0;
$j=0;
$last = 2;
foreach($a as $k=>$v) {
if (!isset($result[$i]))
$result[$i]=array('');
if ( ++$last != $v[1])
$result[$i][$j] .= str_repeat(str_pad(" ",5),($v[1]-$last));
$result[$i][$j] .= $v;
$last = $v[1];
if (substr($v,0,2)=='08') {
$last=3;
$j++;
}
if ($a[$k+1][1]=='3') {
$last=2;
$i++;
$j=0;
}
}
return $result;
}
Watch it live here: http://codepad.org/SWOur3HD version(0.4)
Here is the source code just in case the link dies:
Anyway keep in mind next time you code:
Simple is Beautiful(not anymore now lol)