I have the following array coming from a form submission:
Array ( [id0] => id [gd0] => 50% [q0] => 1 [p0] => 10 [t0] => 10 [id1] => id [gd1] => 65% [q1] => 2 [p1] => 20 [t1] => 40 [id2] => id [gd2] => 90% [q2] => 2 [p2] => 510 [t2] => 1020 )
I want to make it a two dimensional array by storing the same values, in a new pattern like so:
Array (array([id0] => id [gd0] => 50% [q0] => 1 [p0] => 10 [t0] => 10 ) , array([id1] => id [gd1] => 65% [q1] => 2 [p1] => 20 [t1] => 40 ) , array([id2] => id [gd2] => 90% [q2] => 2 [p2] => 510 [t2] => 1020))
Thus I'm trying to rearrange the similar information in a new dimension in an another array. I have tried a foreach loop, but with no luck:
$items = array();
$X = -1; // the index of the first dimension
$Y = 0; // the second index
foreach ($_POST as $val) {
if ($val == 'id') {
$X++;
$Y = 0;
} else {
$items[$X][$Y] == $val;
// increment the second index to prevent overwriting
$Y++;
}
}
print_r($items);
However, it is not working. print_r()
displays only Array()
name="data[0][theKeyForTheField]"
then you will already have everything organized. – prodigitalson Oct 12 '12 at 18:23