V
V
Valera2018-05-16 14:05:39
PHP
Valera, 2018-05-16 14:05:39

How to convert an array in PHP?

Good afternoon everyone.
I'm sitting, racking my brains, hands, keyboard...
You need an initial $oldarray array like this:

$oldarray = Array
(
    [0] => Array
        (
            [DATE] => 01.01.17
            [STATE] => высокий
            [SUM] => 575355
            [PROCENT] => .67
        ),

    [1] => Array
        (
            [DATE] => 01.01.17
            [STATE] => средний
            [SUM] => 82031289
            [PROCENT] => 96.01
        ),

    [2] => Array
        (
            [DATE] => 01.01.17
            [STATE] => низкий
            [SUM] => 2835822
            [PROCENT] => 3.32
        ),

    [3] => Array
        (
            [DATE] => 01.02.17
            [STATE] => высокий
            [SUM] => 540355
            [PROCENT] => .64
        ),

    [4] => Array
        (
            [DATE] => 01.02.17
            [STATE] => средний
            [SUM] => 81360189
            [PROCENT] => 95.87
        ),

    [5] => Array
        (
            [DATE] => 01.02.17
            [STATE] => низкий
            [SUM] => 2964194
            [PROCENT] => 3.49
        ),

    [6] => Array
        (
            [DATE] => 01.03.17
            [STATE] => высокий
            [SUM] => 560355
            [PROCENT] => .66
        ),

    [7] => Array
        (
            [DATE] => 01.03.17
            [STATE] => средний
            [SUM] => 81549849
            [PROCENT] => 95.88
        ),

    [8] => Array
        (
            [DATE] => 01.03.17
            [STATE] => низкий
            [SUM] => 2942863
            [PROCENT] => 3.46
        )

);

result in $newarray like this:
$newarray = Array
(
    [0] => Array
        (
            [DATE] => 01.01.17
            [высокий] => 575355
            [высокий процент] => .67
            [средний] => 82031289
            [средний процент] => 96.01
            [низкий] => 2835822
            [низкий процент] => 3.32
        )

    [1] => Array
        (
            [DATE] => 01.02.17
            [высокий] => 540355
            [высокий процент] => .64
            [средний] => 81360189
            [средний процент] => 95.87
            [низкий] => 2964194
            [низкий процент] => 3.49
        )

    [2] => Array
        (
            [DATE] => 01.03.17
            [высокий] => 560355
            [высокий процент] => .67
            [средний] => 81549849
            [средний процент] => 96.01
            [низкий] => 2942863
            [низкий процент] => 3.32
        )

)

Tried something like this - did not solve the problem:
$newarray = array();
  for ($i = 0; $i < 3; $i++) {
    foreach ($oldarray as $rkey){
      $newarray[$i]['DATE'] = $rkey['DATE'];
      $newarray[$i][$rkey['STATE']] = $rkey['SUM'];
      $newarray[$i][$rkey['PROCENT']] = $rkey['PROCENT'];
    }
  }

Please, help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nick Sdk, 2018-05-16
@epimaster

$newarray = [];
foreach ($oldarray as $item) {
    $newarray[$item['DATE']]['DATE'] = $item['DATE'];
    $newarray[$item['DATE']][$item['STATE']] = $item['SUM'];
    $newarray[$item['DATE']][$item['STATE'] . ' процент'] = $item['PROCENT'];
}

however, in this case, in your new array, the keys will be of the date type ('01.02.17', '01.02.17', ...)
upd: it's better
$newarray = array_values($newarray);

M
Maxim Timofeev, 2018-05-16
@webinar

cast date to integer and use as array key

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question