PHP: Как мне создать диаграмму Морриса из этого массива?

Я пытаюсь написать функцию, которая преобразует мой массив в данные json для использования в диаграммах Морриса. Исходный массив, извлеченный из базы данных, выглядит следующим образом:

    Array
(
[0] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 0
)

[1] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 0
)

[2] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 0
)

[3] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 0
)

[4] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 0
)

[5] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 0
)

[6] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 0
)

[7] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 1
)

[8] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 1
)

[9] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 1
)

[10] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 1
)

[11] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 0
)

[12] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 0
)

[13] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 1
)

[14] => Array
(
[tracking_date] => 2015-08-29
[tracking_isbot] => 1
)

[15] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[16] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[17] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[18] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[19] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[20] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[21] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[22] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[23] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[24] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[25] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[26] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 1
)

[27] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 1
)

[28] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 1
)

[29] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[30] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[31] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[32] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[33] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 1
)

[34] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[35] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[36] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 1
)

[37] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 0
)

[38] => Array
(
[tracking_date] => 2015-08-30
[tracking_isbot] => 1
)

[39] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[40] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[41] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[42] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

[43] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[44] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[45] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[46] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

[47] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

[48] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

[49] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[50] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[51] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[52] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[53] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[54] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

[55] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

[56] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

[57] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

[58] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[59] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[60] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[61] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[62] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[63] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[64] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[65] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

[66] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

[67] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

[68] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[69] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[70] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[71] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[72] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[73] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

[74] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[75] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[76] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 0
)

[77] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

[78] => Array
(
[tracking_date] => 2015-08-31
[tracking_isbot] => 1
)

)

Окончательный вывод JSON должен быть таким, как показано ниже:

    { date: '2015-08-29', human: 9, bots: 6 },
{ date: '2015-08-30', human: 18,  bots: 6 },
{ date: '2015-08-31', human: 26,  bots: 14 }

Из исходного массива:

[date] => xxxx-xx-xx as date:'xxxx-xx-xx'
[isbot] => 0 should be counted as hits from human and
[isbot] => 1 should be counted as hits from bots.

Я создал эту функцию, но не смог вернуть данные в требуемом формате:

function chartData($results) {
$data = array();

foreach($results as $index => $result) {
if($result['tracking_isbot'] == 1) {
if(isset($data[$result['tracking_date']]['bots'])) {
$data[$result['tracking_date']]['bots'] += 1;
} else {
$data[$result['tracking_date']]['bots'] = 1;
}
} else {
if(isset($data[$result['tracking_date']]['human'])) {
$data[$result['tracking_date']]['human'] += 1;
} else {
$data[$result['tracking_date']]['human'] = 1;
}
}
}

return $data;
}

Вышеуказанная функция возвращает массив ниже перед кодированием json:

Array
(
[2015-08-29] => Array
(
[human] => 9
[bots] => 6
)

[2015-08-30] => Array
(
[human] => 18
[bots] => 6
)

[2015-08-31] => Array
(
[human] => 26
[bots] => 14
)

)

json_encode () указанного выше массива:

{"2015-08-29":{"human":9,"bots":6},"2015-08-30":{"human":18,"bots":6},"2015-08-31":{"human":26,"bots":14}}

-1

Решение

Вам также нужно сохранить дату:

function chartData($results) {
$data = array();

foreach($results as $result) {
$t_date = $result['tracking_date'];

if(!isset($data[$t_date])) {
$data[$t_date] = array(
'date' => $t_date,
'human' => 0,
'bots' => 0,
);
}

$data[$t_date]['bots']  += $result['tracking_isbot'];
$data[$t_date]['human'] += (1 - $result['tracking_isbot']);
}

return $data;
}
0

Другие решения

Спасибо @Matteo Tassinari, за ваше решение. Я немного изменил ваш код, и теперь он отлично работает для меня …

function chartData($results) {
$data = array();
$newArray = array();

foreach($results as $result) {
$t_date = $result['tracking_date'];

if(!isset($data[$t_date])) {
$data[$t_date] = array(
'date' => $t_date,
'human' => 0,
'bots' => 0,
);
}

$data[$t_date]['bots']  += $result['tracking_isbot'];
$data[$t_date]['human'] += (1 - $result['tracking_isbot']);
}

foreach($data as $key => $value) {
array_push($newArray, $value);
}

return $newArray;
}
0

По вопросам рекламы [email protected]