将新数组插入多维数组中

时间:2016-06-08 06:14:41

标签: php arrays multidimensional-array

我想在我的multidimensial数组中插入新数组。我按日期划分我的数组,现在我应该插入日期新数组。我写了php,但它插入日期之外。

阵列

Array
(
[2016-05-31 00:00:00] => Array
    (
        [Основной долг] => Array
            (
                [0] => Array
                    (
                        [type_pay] => 0
                        [oper_type] => 4
                        [name_pay] => CLICK
                        [name_oper] => Основной долг
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 1971022
                    )

                [1] => Array
                    (
                        [type_pay] => 1
                        [oper_type] => 4
                        [name_pay] => Наличные
                        [name_oper] => Основной долг
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 564550
                    )

                [2] => Array
                    (
                        [type_pay] => 2
                        [oper_type] => 4
                        [name_pay] => Терминал
                        [name_oper] => Основной долг
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 122714
                    )

            )

        [Депозит] => Array
            (
                [0] => Array
                    (
                        [type_pay] => 1
                        [oper_type] => 3
                        [name_pay] => Наличные
                        [name_oper] => Депозит
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 1175942
                    )

                [1] => Array
                    (
                        [type_pay] => 2
                        [oper_type] => 3
                        [name_pay] => Терминал
                        [name_oper] => Депозит
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 1316410
                    )

            )

        [] => Array
            (
                [0] => Array
                    (
                        [type_pay] => 100
                        [oper_type] => 
                        [name_pay] => Терминал
                        [name_oper] => 
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 843795
                    )

            )

    )

[0] => Array
    (
        [click_count] => test
        [bill_count] => test
        [terminal_count] => test
        [time_pay] => 2016-05-01
        [count_all] => test
        [name_oper] => Итого
    )

)

PHP

foreach ($data["reports"][1] as $value) {
      $bydate[$value['time_pay']][$value['name_oper']][] = $value;
    }

    $calculation =  array (
      'click_count' => 'test',
      'bill_count' => 'test',
      'terminal_count' => 'test',
      'time_pay' => '2016-05-01',
      'count_all' => 'test',
      'name_oper' => 'Итого'
    );

    $bydate[] = $calculation;

我想做什么

Array
(
[2016-05-31 00:00:00] => Array
    (
        [Основной долг] => Array
            (
                [0] => Array
                    (
                        [type_pay] => 0
                        [oper_type] => 4
                        [name_pay] => CLICK
                        [name_oper] => Основной долг
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 1971022
                    )

                [1] => Array
                    (
                        [type_pay] => 1
                        [oper_type] => 4
                        [name_pay] => Наличные
                        [name_oper] => Основной долг
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 564550
                    )

                [2] => Array
                    (
                        [type_pay] => 2
                        [oper_type] => 4
                        [name_pay] => Терминал
                        [name_oper] => Основной долг
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 122714
                    )

            )

        [Депозит] => Array
            (
                [0] => Array
                    (
                        [type_pay] => 1
                        [oper_type] => 3
                        [name_pay] => Наличные
                        [name_oper] => Депозит
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 1175942
                    )

                [1] => Array
                    (
                        [type_pay] => 2
                        [oper_type] => 3
                        [name_pay] => Терминал
                        [name_oper] => Депозит
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 1316410
                    )

            )

        [] => Array
            (
                [0] => Array
                    (
                        [type_pay] => 100
                        [oper_type] => 
                        [name_pay] => Терминал
                        [name_oper] => 
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 843795
                    )

            )

        [Итого] => Array
            (
                [0] => Array
                    (
                        [click_count] => test
                        [bill_count] => test
                        [terminal_count] => test
                        [time_pay] => 2016-05-31
                        [count_all] => test
                        [name_oper] => Итого
                    )

            )

    )

 )

3 个答案:

答案 0 :(得分:1)

您必须为阵列提供索引值。在示例中,您使用Date作为第一个索引,将name_oper作为第二个索引。因为您的数组是静态的,所以您可以使用静态值作为索引值。

$bydate[$value['time_pay']]['Итого'][] = $calculation;

但是如果您的数组是在运行时生成的,那么您必须使用变量作为索引值。

希望这会有所帮助。

答案 1 :(得分:0)

试试这个:

    <Style x:Key="RowNumberCheckBox" TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
        <Setter Property="FlowDirection" Value="RightToLeft"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Margin" Value="5"/>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Grid FlowDirection="LeftToRight">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="20"/>
                            <ColumnDefinition Width="5"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" 
                                   Text="{Binding}"
                                   HorizontalAlignment="Right"/>
                    </Grid>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Content" Value="{Binding Number}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
    </Style>

答案 2 :(得分:0)

尝试使用内置array_push函数的php。为array_push

指定该值的索引
array_push($arr['index'], $newArr);