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>";
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;
}
You can just loop and use the client's identifier as an array key:
foreach($submissions as $d)
{
if(isset($counts[$d['client']]))
$counts[$d['client']] = 1;
else
$counts[$d['client']]+=1;
}
You can do it this way:
$array = array();
foreach($submissions as $submission){
$array[$submission['client']]['submitted'] = isset($array[$submission['client']]['submitted'])? ($array[$submission['client']]['submitted'] + 1): 1;
}