Reputation: 19356
I have an associative array with the following contents:
Array
(
[L_TIMESTAMP0] => 2011%2d10%2d16T16%3a57%3a38Z
[L_TIMESTAMP1] => 2011%2d10%2d16T16%3a45%3a23Z
[L_TIMESTAMP2] => 2011%2d10%2d16T16%3a35%3a54Z
[L_TIMEZONE0] => GMT
[L_TIMEZONE1] => GMT
[L_TIMEZONE2] => GMT
[L_STATUS0] => Completed
[L_STATUS1] => Completed
[L_STATUS2] => Completed
[TIMESTAMP] => 2011%2d10%2d16T17%3a58%3a39Z
)
What I want to do is to 'group' all instances of L_TIMESTAMP0, L_TIMEZONE0 and L_STATUS0 (and L_TIMESTAMP1, L_TIMEZONE1, L_STATUS1) into their own array, or as part of a multidimensional array. But I simply can't seem to figure out how I would go about accomplishing this.
The expected results would be:
Array
(
[L_TIMESTAMP0] => 2011%2d10%2d16T16%3a57%3a38Z
[L_TIMEZONE0] => GMT
[L_STATUS0] => Completed
)
Array
(
[L_TIMESTAMP1] => 2011%2d10%2d16T16%3a45%3a23Z
[L_TIMEZONE1] => GMT
[L_STATUS1] => Completed
)
I figured this would be simple with a simple for-loop, but that doesn't give me the results I'm looking for.
Any ideas?
It's likely something very obvious, but I've spent the last few days with this and I can't figure it out (although that probably says more about my skills than the problem)
edit:
To add; I don't need the exact code for my particular question, but any pointers as to what direction I should look into would be greatly appreciated.
Upvotes: 0
Views: 126
Reputation: 11711
$restructuring = true;
$count = 0;
$new_array = array();
while($restructuring)
{
$restructuring = false;
foreach($original_array as $oa)
{
if(!array_key_exists($count, $new_array) && array_key_exists('L_TIMESTAMP'.$count, $original_array))
{
$new_element = array();
foreach(array('TIMESTAMP', 'TIMEZONE', 'STATUS') as $e)
if(!empty($original_array['L_'.$e.$count])) $new_element[$e.$count] = $original_array['L_'.$e.$count];
$new_array[] = $new_element;
$count++;
$restructuring = true;
}
}
}
Given $original_array as defined in the question, the $new_array will be:
Array
(
[0] => Array
(
[TIMESTAMP0] => 2011%2d10%2d16T16%3a57%3a38Z
[TIMEZONE0] => GMT
[STATUS0] => Completed
)
[1] => Array
(
[TIMESTAMP1] => 2011%2d10%2d16T16%3a45%3a23Z
[TIMEZONE1] => GMT
[STATUS1] => Completed
)
[2] => Array
(
[TIMESTAMP2] => 2011%2d10%2d16T16%3a35%3a54Z
[TIMEZONE2] => GMT
[STATUS2] => Completed
)
)
Upvotes: 0
Reputation: 140236
<?php
$groupRegex = '/([0-9]+)$/';
$multiArray = array();
$assocArray = array (
"L_TIMESTAMP0" => "2011%2d10%2d16T16%3a57%3a38Z",
"L_TIMESTAMP1" => "2011%2d10%2d16T16%3a45%3a23Z",
"L_TIMESTAMP2" => "2011%2d10%2d16T16%3a35%3a54Z",
"L_TIMEZONE0" => "GMT",
"L_TIMEZONE1" => "GMT",
"L_TIMEZONE2" => "GMT",
"L_STATUS0" => "Completed",
"L_STATUS1" => "Completed",
"L_STATUS2" => "Completed",
"TIMESTAMP" => "2011%2d10%2d16T17%3a58%3a39Z"
);
foreach( $assocArray as $key => $value ) {
preg_match( $groupRegex, $key, $group );
if( !isset($group[1]) ){
continue;
}
$group = intval( $group[1] );
if( is_array( $multiArray[$group] ) ) {
$multiArray[$group][$key] = $value;
}
else {
$multiArray[$group] = array( $key => $value );
}
}
echo "<pre>";
print_r( $multiArray);
echo "</pre>";
?>
result
Array
(
[0] => Array
(
[L_TIMESTAMP0] => 2011%2d10%2d16T16%3a57%3a38Z
[L_TIMEZONE0] => GMT
[L_STATUS0] => Completed
)
[1] => Array
(
[L_TIMESTAMP1] => 2011%2d10%2d16T16%3a45%3a23Z
[L_TIMEZONE1] => GMT
[L_STATUS1] => Completed
)
[2] => Array
(
[L_TIMESTAMP2] => 2011%2d10%2d16T16%3a35%3a54Z
[L_TIMEZONE2] => GMT
[L_STATUS2] => Completed
)
)
Upvotes: 3