我原来的数组是:
Array
(
[0] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-29
)
[1] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-29
)
[2] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-29
)
[3] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-29
)
[4] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-29
)
[5] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-29
)
[6] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-29
)
[7] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-29
)
[8] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-29
)
[9] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-29
)
[10] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-29
)
[11] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-29
)
[12] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-29
)
[13] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-29
)
[14] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-29
)
[15] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[16] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[17] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[18] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[19] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[20] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[21] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[22] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[23] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[24] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[25] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[26] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-30
)
[27] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-30
)
[28] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-30
)
[29] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[30] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[31] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[32] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[33] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-30
)
[34] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[35] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[36] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-30
)
[37] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-30
)
[38] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-30
)
[39] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[40] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[41] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[42] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
[43] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[44] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[45] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[46] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
[47] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
[48] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
[49] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[50] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[51] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[52] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[53] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[54] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
[55] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
[56] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
[57] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
[58] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[59] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[60] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[61] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[62] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[63] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[64] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[65] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
[66] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
[67] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
[68] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[69] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[70] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[71] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[72] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[73] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
[74] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[75] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[76] => Array
(
[tracking_isbot] => 0
[tracking_date] => 2015-08-31
)
[77] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
[78] => Array
(
[tracking_isbot] => 1
[tracking_date] => 2015-08-31
)
)
我试图创建一个返回一个新数组的函数,后者可以在图表中使用,我创建的函数代码如下:
function chartData($results) {
$data = array();
$bots = 1;
$human = 1;
foreach($results as $subresults) {
if($subresults['tracking_isbot'] === 1) {
$data['bots'][$subresults['tracking_date']] = $bots++;
} else {
$data['human'][$subresults['tracking_date']] = $human++;
}
}
return $data;
}
我从上述功能中获取的数据不正确:
Array
(
[human] => Array
(
[2015-08-29] => 9
[2015-08-30] => 27
[2015-08-31] => 53
)
[bots] => Array
(
[2015-08-29] => 6
[2015-08-30] => 12
[2015-08-31] => 26
)
)
你能否告诉我这里我做错了什么......
答案 0 :(得分:0)
好吧,我想你的计数是不正确的,这对我来说绝对有意义,因为你总结了所有内容并把它放入数组......
我试着解释一下: 你每次被击中时都会增加$ bot和$ human值并将其推入值中。这总结得非常快,并且此时会产生一个帐户 - 而不是此日期的数据计数。
试试这个:
function chartData($results) {
$data = array();
foreach($results as $subresults) {
$date = $subresults['tracking_date'];
$key = 'human';
if($subresults['tracking_isbot'] === 1) {
$key = 'bots';
}
if(!array_key_exists($date, $data[$key]))
$data[$key][$date] = 1;
else
$data[$key][$date]++;
}
return $data;
}
希望这有帮助
答案 1 :(得分:0)
function chartData($results) {
$newArray = array('human' => array(), 'bots' => array());
foreach($results as $index => $result) {
if($result['tracking_isbot'] == 1) {
if(isset($newArray['bots'][$result['tracking_date']])) {
$newArray['bots'][$result['tracking_date']] += 1;
} else {
$newArray['bots'][$result['tracking_date']] = 1;
}
} else {
if(isset($newArray['human'][$result['tracking_date']])) {
$newArray['human'][$result['tracking_date']] += 1;
} else {
$newArray['human'][$result['tracking_date']] = 1;
}
}
}
return $newArray;
}
您尝试计算每tracking_date
个人/机器人的数量,如果我正确,那么上述功能就可以完成。