在数组PHP中计算项目

时间:2014-06-17 14:50:08

标签: php arrays

我无法计算我的阵列如何帮助他人。

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
                )

        )

)

我希望在创建它之后过滤此数组,看起来像这样。

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

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

                )
)

目前我的代码看起来像这样,我猜我需要另一个foreach来过滤这个更多,但是我坚持使用这个数组。

    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 个答案:

答案 0 :(得分:1)

您可以循环使用客户端的标识符作为数组键:

foreach($submissions as $d)
{
    if(isset($counts[$d['client']]))
        $counts[$d['client']] = 1;
    else
        $counts[$d['client']]+=1;
}

答案 1 :(得分:1)

像这样的东西

$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 :(得分:0)

你可以这样做:

 $array = array();
 foreach($submissions as $submission){
       $array[$submission['client']]['submitted'] = isset($array[$submission['client']]['submitted'])? ($array[$submission['client']]['submitted'] + 1): 1;
 }