0

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

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.