如何使普通数组成为基于第一项的维数组?

时间:2015-07-27 14:58:13

标签: php arrays sorting merge

下面是我的数组在for-each循环中回显。

Array
(
    [1] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 2
    )
   [2] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 1
    )
)
Array
(
    [3] => Array
    (
        [0] => 2
        [1] => 1
        [2] => 1
    )
)
Array
(
    [4] => Array
    (
        [0] => 3
        [1] => 3
        [2] => 1
    )
    [5] => Array
    (
        [0] => 3
        [1] => 3
        [2] => 3
    )
    [6] => Array
    (
        [0] => 3
        [1] => 3
        [2] => 2
    )
)
Array
(
    [7] => Array
    (
        [0] => 4
        [1] => 2
        [2] => 2
    )
    [8] => Array
    (
        [0] => 4
        [1] => 2
        [2] => 1
    )
 )

我想根据第一项(index = 0)的值对这些数组进行分组,并得到以下维数组。

 <Style x:Key="SlideControlGrid" TargetType="{x:Type UserControl}">
    <Style.Triggers>
        <EventTrigger RoutedEvent="UserControl.MouseEnter">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Duration="00:00:00.2"
                                    From="#FFFFFFFF"
                                    Storyboard.TargetProperty="Background.Color"
                                    To="#FFFCEE" />
                </Storyboard>

            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="UserControl.MouseLeave">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Duration="00:00:00.2"
                                    Storyboard.TargetProperty="Background.Color"
                                    To="#FFFFFFFF" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="UserControl.PreviewDragOver">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Duration="00:00:00.05"
                                    Storyboard.TargetProperty="Background.Color"
                                    To="#FFD86C" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="UserControl.PreviewDragLeave">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Duration="00:00:00.2"
                                    Storyboard.TargetProperty="Background.Color"
                                    To="#FFFFFFFF" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Style.Triggers>
</Style>

我花了好几个小时来解决这个问题,但由于经验不足,我仍然无法做到这一点。请帮我一些algoritme。

1 个答案:

答案 0 :(得分:3)

不会那样做吗?

$result = [];
foreach ($originalArrays as $array) {
  $result[$array[0]][] = $array;
}