Thank you for any help. This one has me stumped. We start with this:
Array
(
[numCols] => 8
[timePointLabel1] => Week1
[timePointLabel4] => Week2
[timePointLabel7] => Week3
[0] => Array
(
[Location0_name] => Name1
[colText01] => 2
[colText07] => 4
)
[1] => Array
(
[Location1_name] => Name2
[colText11] => 9
[colText14] => 7
)
)
I want the NNN in the timePointLabelsNNNs to go in numeric order, starting at 0. Currently the NNNs are 1, 4, 7.
We also have an array within an array. The N in the colTextN fields are related to the time point labels. E.g. timePointLabel4 is associated with colText04, colText14, and any other colText that ends in a single digit of 4.
I figured out how to dynamically reorder the NNNs and so I am getting this:
Array
(
[numCols] => 8
[timePointLabel0] => Week1
[timePointLabel1] => Week2
[timePointLabel2] => Week3
[0] => Array
(
[Location0_name] => Name1
[colText01] => 2
[colText07] => 4
)
[1] => Array
(
[Location1_name] => Name2
[colText11] => 9
[colText14] => 7
)
)
See the NNNs in the timePointLabelNNNs are now 0, 1, 2.
How did I do this? With this code:
$timePointLabelCount = preg_grep("/^timePointLabel(\d)+$/",array_keys($this->data));
// go through the time point label array and create a new array to use
// it is assigned the correct order of keys, starting with 0 (since arrays start with 0 anyway)
foreach ($timePointLabelCount as $timePointCustom) {
$timePointCustomArray[] = $this->data[$timePointCustom];
unset($this->data[$timePointCustom]);
}
$timePointNum = 0;
// insert the correct timepoint data, in the correct order, into the array
foreach ($timePointCustomArray as $setTimePointData) {
$this->data['timePointLabel' . $timePointNum] = $setTimePointData;
$timePointNum++;
}
But the colTexts are still an issue. For the N values in colTextN, anything that is 1 should now be a 0, the 4 should now be a 1, and the 7s should now be a 2.
So I want my final array to look like this:
Array
(
[numCols] => 8
[timePointLabel0] => Week1
[timePointLabel1] => Week2
[timePointLabel2] => Week3
[0] => Array
(
[Location0_name] => Name1
[colText00] => 2
[colText02] => 4
)
[1] => Array
(
[Location1_name] => Name2
[colText10] => 9
[colText11] => 7
)
)
I suspect this will require some clever usage of a foreach loop.
You can not rename keys w/o tainting the order in the same array, but you could build a new array (and unset the previous one later on):
$i = 0;
$search = 'timePointLabel';
$reordered = array();
foreach($array as $key => &$value)
{
if (0 === strpos($key, $search))
{
$key = $search . $i++;
}
$reordered[$key] =& $value;
}
unset($value);
$array = &$reordered;
unset($reordered);
print_r($array);
Edit: To solve this for the sub-keys, you need to obtain the mapping of the digits and apply those the same way on the sub-array. The principle is actually identical + using the map:
$i = 0;
$search = 'timePointLabel';
$subsearch = 'colText';
$reordered = array();
$map = array();
foreach($array as $key => &$value)
{
if (0 === strpos($key, $search))
{
$digit = NULL;
sscanf($key, $search.'%d', $digit);
if (NULL === $digit || $digit > 9) throw new Exception(sprintf('Invalid Key %d.', $key));
$map[$digit] = $i;
$key = $search . $i++;
}
elseif (is_int($key) && is_array($value))
{
$subreordered = array();
foreach($value as $subkey => &$subvalue)
{
if (0 === strpos($subkey, $subsearch))
{
$lastPos = strlen($subkey)-1;
$last = $subkey[$lastPos];
if (isset($map[$last]))
{
$subkey[$lastPos] = $map[$last];
}
}
$subreordered[$subkey] = &$subvalue;
}
unset($subvalue);
$value =& $subreordered;
unset($subreordered);
}
$reordered[$key] =& $value;
}
unset($value);
unset($map);
$array = &$reordered;
unset($reordered);
print_r($array);