0

I want to insert new array into my multidimensial array. I divided my array by date and now I should insert into date new array. I wrote php however it insert outside of the date.

Array

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;

What I want to do

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 Answers 3

1

You Have to provide the Index value to your array. In the example you are using the Date as first index and name_oper as second index. Because your array is static you can use static values as index values.

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

but if your array is generated on the run time then you have to use variable for your index values.

Hope this will help.

Sign up to request clarification or add additional context in comments.

Comments

0

Try This:

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[$value['time_pay']]['Итого'][0] = $calculation;

Comments

0

Try php built in array_push function. specify the index of that value to array_push

array_push($arr['index'], $newArr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.