0

I'm a complete newbie at php, but anyway, here it goes:

I want to count the amount of ["completed"]=> bool(true) in the array below.

I was able to count the total by doing this: $totalCount = count($object->data);. Do I need to do a foreach() in order to get the amount of completed ones or can I get around it by just doing some kind of count()?

Thanks!

object(stdClass)#2 (1) {
  ["data"]=>
  array(232) {
    [0]=>
    object(stdClass)#3 (2) {
      ["id"]=>
      int(13081073106396)
      ["completed"]=>
      bool(true)
    }
    [1]=>
    object(stdClass)#4 (2) {
      ["id"]=>
      int(13160080793822)
      ["completed"]=>
      bool(false)
    }
2
  • Looks like this has already been answered: [stackoverflow.com/questions/6291027/… [1]: stackoverflow.com/questions/6291027/… Commented Feb 9, 2015 at 11:34
  • You can use the answer that @gratz is referring to, just remember to reference object properties with the -> operator, so it is $obj->data and not $obj['data'] because you are dealing with objects and not arrays. Commented Feb 9, 2015 at 11:37

3 Answers 3

0

I think something like that might work:

var $count = 0;
foreach($stdClass->data as $obj)
    if($obj->completed)
        $count++;
Sign up to request clarification or add additional context in comments.

Comments

0

Consider $oObject is your object

$array = json_decode(json_encode($oObject),true);

then apply simple array count like

echo count($array);

It will return total count of data. Hope this will help!

Comments

0

Use a callback to count:

$countCompleted = 0;

array_map(function ($n) use $countCompleted {
    if ( $n->completed === true )
         $countCompleted ++;
}, $objectMap->data );

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.