請問怎麼在PHP多維陣列中篩選出自己想要的數值並輸出

來源:互聯網
上載者:User
請教如何在PHP多維陣列中篩選出自己想要的數值並輸出?
我糾結了好久的一個問題,請大牛賜教!

有這樣一個數組



Array(
[0] => Array (
[log_data] => 4
[log_result] => 0
[log_time] => 2013-05-08 10:08:40 )
[1] => Array (
[log_data] => 10018
[log_result] => 2
[log_time] => 2013-05-08 10:13:58 )
[2] => Array (
[log_data] => 10018
[log_result] => 2
[log_time] => 2013-05-08 10:14:02 )
[3] => Array (
[log_data] => 10003
[log_result] => 50
[log_time] => 2013-05-08 10:18:29 )
)




上面僅列出3個,但實際上可能有幾千個。


每個數組的log_time可能是不一樣的。

我需要在HTML中按照日期篩選我想要的內容並迴圈輸出該如何做呀?



我想輸出的內容如下:

(行為不同值的log_data,列為log_time的年月日,值x為log_data以及log_time匹配的所有數組內的log_result的總和)?

2013-05-08 2013-05-09 2013-05-10?

log_data1 值x 值x 值x??

log_data2? 值x 值x 值x

log_data3? 值x 值x 值x

求教。
------解決方案--------------------
$ar = Array( 
Array (
'log_data' => 4,
'log_result' => 0,
'log_time' => '2013-05-08 10:08:40'
),
Array (
'log_data' => 10018,
'log_result' => 2,
'log_time' => '2013-05-08 10:13:58',
),
Array (
'log_data' => 10018,
'log_result' => 2,
'log_time' => '2013-05-08 10:14:02',
),
Array (
'log_data' => 10003,
'log_result' => 50,
'log_time' => '2013-05-08 10:18:29',
),
);
foreach($ar as $item) {
@$res[$item['log_data']][strtok($item['log_time'], ' ')] += $item['log_result'];
}
print_r($res);
Array
(
[4] => Array
(
[2013-05-08] => 0
)

[10018] => Array
(
[2013-05-08] => 4
)

[10003] => Array
(
[2013-05-08] => 50
)

)


------解決方案--------------------

$ar = Array(
Array (
'log_data' => 4,
'log_result' => 0,
'log_time' => '2013-05-08 10:08:40'
),
Array (
'log_data' => 10018,
'log_result' => 2,
'log_time' => '2013-05-08 10:13:58',
),
Array (
'log_data' => 10018,
'log_result' => 2,
'log_time' => '2013-05-08 10:14:02',
),
Array (
'log_data' => 10003,
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.