Answer the question
In order to leave comments, you need to log in
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
)
);
$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
)
)
$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'];
}
}
Answer the question
In order to leave comments, you need to log in
$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'];
}
$newarray = array_values($newarray);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question