在assoc中对项目进行分组。排列

时间:2011-10-16 18:07:58

标签: php arrays

我有一个关联数组,其中包含以下内容:

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
)

我想要做的是将L_TIMESTAMP0,L_TIMEZONE0和L_STATUS0(以及L_TIMESTAMP1,L_TIMEZONE1,L_STATUS1)的所有实例“分组”到它们自己的数组中,或者作为多维数组的一部分。但我似乎无法弄清楚我将如何实现这一目标。

预期结果将是:

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
    )

我认为这对于简单的for循环来说很简单,但这并没有给我我想要的结果。

任何想法?
这可能是非常明显的事情,但我已经花了最后几天这个,我无法弄明白(虽然这可能更多地说明我的技能而不是问题)

编辑:
加上;我不需要我的特定问题的确切代码,但任何关于我应该研究的方向的指示将非常感激。

2 个答案:

答案 0 :(得分:3)

<?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>";
?>

结果

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
    )

)

答案 1 :(得分:0)

$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;
    }
  }
}

根据问题中定义的$ original_array,$ new_array将是:

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
        )

)