在PHP中重新排序多维数组

时间:2013-08-14 09:00:54

标签: php arrays sorting multidimensional-array

我有一个需要重新排序的多维数组。

我需要对数组进行排序,以便首先考虑LEVEL,然后是SECTION,其中SECTION等于前一个元素的ID。

有人能帮助我吗? 谢谢。 这是数组:

Array
(
    [0] => Array
        (
            [LEVEL] => 1
            [ID] => 1_1
            [SECTION] => _
        )
    [1] => Array
        (
            [LEVEL] => 1
            [ID] => 1_2
            [SECTION] => _
        )
    [2] => Array
        (
            [LEVEL] => 2
            [ID] => 2_1
            [SECTION] => 1_1
        )
    [3] => Array
        (
            [LEVEL] => 2
            [ID] => 2_2
            [SECTION] => 1_2
        )
    [4] => Array
        (
            [LEVEL] => 3
            [ID] => 3_1
            [SECTION] => 2_1
        )
    [5] => Array
        (
            [LEVEL] => 3
            [ID] => 3_2
            [SECTION] => 2_2
        )

这是我需要的结果:

Array
(
    [0] => Array
        (
            [LEVEL] => 1
            [ID] => 1_1
            [SECTION] => _
        )
    [2] => Array
        (
            [LEVEL] => 2
            [ID] => 2_1
            [SECTION] => 1_1
        )
    [4] => Array
        (
            [LEVEL] => 3
            [ID] => 3_1
            [SECTION] => 2_1
        )
    [1] => Array
        (
            [LEVEL] => 1
            [ID] => 1_2
            [SECTION] => _
        )
    [3] => Array
        (
            [LEVEL] => 2
            [ID] => 2_2
            [SECTION] => 1_2
        )
    [5] => Array
        (
            [LEVEL] => 3
            [ID] => 3_2
            [SECTION] => 2_2
        )

3 个答案:

答案 0 :(得分:1)

如果我错了,请纠正我,但看起来排序所需的所有信息都在ID中。组包含在下划线后面的数字中,以及前面数字中的级别。因此,可以通过颠倒这些数字的顺序并执行自然顺序字符串比较来确定顺序:

1_1, 2_1, 3_1, 4_1, 1_2, 2_2, 3_2, 1_3, 2_3, 3_3, 4_3, etc.

成为:

1-1, 1-2, 1-3, 1-4, 2-1, 2-2, 2-3, 3-1, 3-2, 3-3, 3-4, etc.

因此,如果您的初始数组被称为$arr

$ord = array_map(function($a) {return $a['ID'];}, $arr);
$ord = preg_replace('/(\d+)_(\d+)/', '$2-$1', $ord);
array_multisort($ord, SORT_NATURAL, $arr);

如果您使用的是PHP 5.5:

$ord = preg_replace('/(\d+)_(\d+)/', '$2-$1', array_column($arr, 'ID'));
array_multisort($ord, SORT_NATURAL, $arr);

答案 1 :(得分:0)

处理您的问题很有意思,请在您的查询中查看我的以下解决方案, 它对我来说很好:

$arrData=array(
            array(
                'LEVEL'=>1,
                'ID'=>'1_1',
                'SECTION'=>''                       
                ),

            array(
                    'LEVEL'=>'3',
                    'ID'=>'3_1',
                    'SECTION'=>''
            ),
            array(
                    'LEVEL'=>'3',
                    'ID'=>'3_2',
                    'SECTION'=>''
            ),
            array(
                    'LEVEL'=>2,
                    'ID'=>'2_1',
                    'SECTION'=>''
            ),

        array(
                'LEVEL'=>1,
                'ID'=>'1_2',
                'SECTION'=>''
        ),
        );

$arrLevels=array();
foreach($arrData as $key=>$val)
{
    if(!in_array($val['LEVEL'], $arrLevels))
    {
        $arrLevels[]=$val['LEVEL'];
    }
}

 sort($arrLevels);

$arrDataNew=array();
$arrSortIndexes=array();
do
{
    foreach ($arrLevels as $level)
    {
        //array_walk($arrData, 'sort_data',$level,$arrSortIndexes,$arrData);
        $arrCurrentLevel=array();
        foreach($arrData as $key=>$dataC)
        {
            if($dataC['LEVEL']==$level)
            {
                $arrCurrentLevel[intval(end(explode('_',$dataC['ID'])))]=$key;
            }
        }
        if(sizeof($arrCurrentLevel)>0)
        {
            $keyOfArrData=$arrCurrentLevel[min(array_keys($arrCurrentLevel))];
            $arrDataNew[]=$arrData[$keyOfArrData];
            unset($arrData[$keyOfArrData]);
        }


    }

}while(sizeof($arrData)>0);

print_r($arrDataNew);

答案 2 :(得分:0)

我终于以这种方式解决了,它不是最快的解决方案,但它至少可以起作用

        $i=0;
        foreach ($polozky as $id => $polozka) 
        {
          if ($polozka["LEVEL"] == 1) 
          {
            $i++;
          }
        }

        $j=0;
        foreach ($polozky as $id => $polozka) 
        {
          if ($polozka["LEVEL"] == 2) 
          {
            $j++;
          }
        }

        $k=0;
        foreach ($polozky as $id => $polozka) 
        {
          if ($polozka["LEVEL"] == 3) 
          {
            $k++;
          }
        }

        $stack = array();
        for($l = 0; $l < $i; ++$l)
        {
          array_push($stack, $polozky[$l]);
          $id1 = $polozky[$l]["ID"];

           for($m = 0; $m < $j+$i; ++$m)
            {
              if($polozky[$m]["SECTION"] == $id1)
              {
               array_push($stack, $polozky[$m]);

                $id2 = $polozky[$m]["ID"];
                for($n = 0; $n < $k+$j+$i; ++$n)
                {
                  if($polozky[$n]["SECTION"] == $id2)
                  {
                    array_push($stack, $polozky[$n]);
                  }
                }
              }
            } 
        }