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月17日T14:57:08+0100
)
[1] => Array
(
[id] => 40403
[client] => Client 1
[date] => 2014年06月17日T14:39:02+0100
)
[2] => Array
(
[id] => 40402
[client] => Client 2
[date] => 2014年06月17日T13: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>";
asked Jun 17, 2014 at 14:50
Brent
2,50310 gold badges41 silver badges64 bronze badges
3 Answers 3
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;
}
answered Jun 17, 2014 at 14:56
Unlink
1,0031 gold badge8 silver badges14 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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;
}
answered Jun 17, 2014 at 14:54
user3232725
2181 silver badge6 bronze badges
Comments
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;
}
answered Jun 17, 2014 at 14:57
Think Different
2,8251 gold badge14 silver badges18 bronze badges
Comments
lang-php