在PHP中关联多维数组中的数据

时间:2011-10-10 20:58:15

标签: php arrays multidimensional-array

感谢您的帮助。这个让我难过。我们从这开始:

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
    )
)

我希望timePointLabelsNNNs中的NNN按数字顺序排列,从0开始。目前NNN是1,4,7。

我们在数组中也有一个数组。 colTextN字段中的N与时间点标签相关。例如。 timePointLabel4与colText04,colText14以及以4的单个数字结尾的任何其他colText相关联。

我想出了如何动态重新排序NNN,所以我得到了这个:

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
    )
)

请参阅timePointLabelNNNs中的NNN现在为0,1,2。

我是怎么做到的?使用此代码:

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

但是colTexts仍然是一个问题。对于colTextN中的N值,1的任何值现在应该是0,4现在应该是1,而7s现在应该是2。

所以我希望我的最终数组看起来像这样:

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
    )
)

我怀疑这需要巧妙地使用foreach循环。

1 个答案:

答案 0 :(得分:0)

你不能重命名没有在同一个数组中修改顺序的键,但是你可以构建一个新数组(并在以后取消设置前一个数组):

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

Demo

编辑:要解决此问题,需要获取数字的映射并在子数组中以相同的方式应用这些数字。原则实际上是相同的+使用地图:

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