排序

时间:2015-07-27 17:48:08

标签: php arrays multidimensional-array

我有以下数组:

Array
(
[0] => Array
    (
        [0] => 2015-07-18
        [1] => 22 SSH
    )

[1] => Array
    (
        [0] => 2015-07-18
        [1] => 80 HTTP
    )

[2] => Array
    (
        [0] => 2015-07-18
        [1] => 3389 Remote Desktop
    )

[3] => Array
    (
        [0] => 2015-07-19
        [1] => 3389 Remote Desktop
    )

[4] => Array
    (
        [0] => 2015-07-19
        [1] => 3389 Remote Desktop
    )
)

以及以下函数将数据以高图表的形式提供所需的格式/数组:

$result = array();
$result[0] = array();
$result[0][data] = array();
foreach ($stack_stats_timeline as $key => &$value) {
    if(!in_array($value[0], $result[0][data], true)) array_push($result[0][data], $value[0]);  
    $hash = $value[1];
    $result[$hash][name] = $value[1];
    $result[$hash][data][$value[0]] += 1;  
}   

到目前为止一切都那么好......不过问题在于我什么时候

$result = json_encode($result);
print_r($result); 

我得到了

[{"data":["2015-07-01","2015-07-02","2015-07-03"]},{"name":"8080 Unknown","data":{"2015-07-01":4,"2015-07-02":8,"2015-07-03":5}},{"name":"8118 Unknown","data":{"2015-07-01":3}},{"name":"3389 Remote Desktop","data":{"2015-07-01":14,"2015-07-02":52,"2015-07-03":65}},{"name":"80 HTTP","data":{"2015-07-01":3,"2015-07-02":12,"2015-07-03":7}},{"name":"8228 Unknown","data":{"2015-07-01":3}}]  

当格​​式为:

时,问题出现在数据中
{"key":number,"key":number}

这应该只是:

{number,number}

问题:在按日期对出现事项进行排序后,如何删除数组键?

1 个答案:

答案 0 :(得分:1)

我可能会做一些事情:

$headings = $result[0]['data'];
for ($i = 0; $i < count($result[1]); $i ++) {
    $data = $result[1][$i]['data'];
    $newdata = array();
    foreach($headings as $key)
        $newdata[] = isset($data[$key]) ? $data[$key] : 0;
    $result[1][$i]['data'] = $newdata;
}
相关问题