0

Anyone know what I'm doing wrong?

From the following array I want to count the the number of times "Friday" occurs.

Array ( 
[404979509517702] => Array ( 
    [0] => 235 
    [1] => 04:10,Friday 
) 
[404045862944400] => Array ( 
    [0] => 192 
    [1] => 23:52,Wednesday 
) 
[20403274909688162] => Array ( 
    [0] => 186 
    [1] => 22:21,Tuesday 
) 
[202735273075459] => Array ( 
    [0] => 336 
    [1] => 04:29,Tuesday 
) 
[652948031457462] => Array ( 
    [0] => 410 
    [1] => 06:22,Monday 
) 
[2606749954978] => Array ( 
    [0] => 312 
    [1] => 05:01,Saturday 
) 
[755318061725] => Array ( 
    [0] => 384 
    [1] => 04:51,Friday 
)
)

This is what I'm doing:

$friday = array();
foreach ($the_array as $friday){
    $the_array = explode(',', $friday[1]);

    $the_array[$the_array [1]] += ($friday[1]);
}

print_r($friday);

This way, I get the wrong number of "Friday" occurrences. Any idea what I'm doing wrong or if there is a more elegant solution to that?

5 Answers 5

3

I would try something like the following if all you want to do is count occurrences:

$friday_count = 0;
foreach($the_array as $record) {
    // Search for "Friday"
    if(stristr($record[1],'Friday') !== false) {
        $friday_count++;    // increment count
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

$fridayCount = 0;
foreach ($the_array as $friday){
    $friArr = explode(',', $friday[1]);
    if($friArr[1] == "Friday") {
      $fridayCount++;
    }
}

echo ($fridayCount);

Comments

2

The code should be:

$fridayCount = 0;

foreach ($the_array as $friday){
    if(strrpos($friday[1], 'Friday') !== false) {
       $fridayCount++;
    }
}

echo $fridayCount;

2 Comments

Line 4: if(strrpos($friday[1], 'Friday') !== false) {
Just tested your code (with ' in line 4), works fine! Thanks.
1

I think you might be complicating this unnecessarily. You should be able to test for 'Friday' and increment a counter ($friday, here) as follows:

$friday = 0;
foreach ($the_array as $element){
  if (array_pop(explode(',', $element[1])) == 'Friday') {
    $friday++;
  }
}

echo $friday;

4 Comments

Note that I'm not trimming any trailing spaces after exploding the day off of $element[1]--if that's a concern, you may need to wrap a trim in there before testing == 'Friday'
Hi @rjz, thanks for your answer. I tried implementing your code but when printing it I get "0". Any ideas why that happens? Trailing spaces won't be a concern but thanks for the good thought!
You are comparing array with string on line 3.
Thanks! Saw that [1] in there and called it good--should be better now.
-2
$cnt = 0;
foreach ($the_array as $friday){
  if (explode(',', $friday[1]) == 'Friday') {
    $cnt++;
  }
}
echo $cnt;

Comments

Your Answer

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.