douyin2962 2014-06-17 14:50
浏览 16
已采纳

在数组PHP中计算项目

I'm having trouble counting my arrays how someone can help.

Array
(
    [2014-06-17] => Array
        (
            [0] => Array
                (
                    [id] => 40404
                    [client] => Client 1
                    [date] => 2014-06-17T14:57:08+0100
                )

            [1] => Array
                (
                    [id] => 40403
                    [client] => Client 1
                    [date] => 2014-06-17T14:39:02+0100
                )

            [2] => Array
                (
                    [id] => 40402
                    [client] => Client 2
                    [date] => 2014-06-17T13:34:18+0100
                )

        )

)

I would like filter this array after I created it to look like this.

Array
(
    [2014-06-17] => Array
        (
            [Client 1] => Array
                (
                    [submitted] => 2
                )

            [Client 2] => Array
                (
                    [submitted] => 1

                )
)

Currently my code looks like this, i'm guessing I need another foreach to filter this more but i'm stuck filting this array.

    foreach ($submissions as $sortArray) {
        $dataJson[substr($sortArray['thing']['created'], 0, 10)][] = array(
            'id' => $sortArray['id'],
            'client' => $sortArray['thing']['client']['name'],
            'date' => $sortArray['thing']['created']
        );
        $filterd = $dataJson;
    }

    echo "<pre>";
    print_r($filterd);
    echo "</pre>";
  • 写回答

3条回答 默认 最新

  • duanmo6937 2014-06-17 14:56
    关注

    Something like this

    $result = array();
    
    foreach($source as $day => $orders) {
        $clients = array();
        foreach ($orders as $order) {
            if (!isset($clients[$order['client']])) {
                $clients[$order['client']] = array('submitted' => 1);
            }
            else {
                $clients[$order['client']]['submitted']++;
            }
        }
        $result[$day] = $clients;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭