I work with PHP arrays
and objects
. I have been working with them for a long time. However, I am stuck in a problem that may have a very simple solution.
I have a variable $products
inside a function that gets a value when called. I am trying to count the objects in a variable to see how many products are inside it. I tried a simple function count($products)
and count((array)$products)
and it does not work. I know this is not the best way to count objects.
Is there any way to count them?
object(stdClass)#46 (3) {
["0"]=>
object(stdClass)#47 (1) {
["productid"]=>
string(2) "15"
}
["1"]=>
object(stdClass)#48 (1) {
["productid"]=>
string(2) "16"
}
["2"]=>
object(stdClass)#48 (1) {
["productid"]=>
string(2) "26"
}
}
I need this to return 3
object(stdClass)#20 (1) {
["productid"]=>
string(2) "21"
}
I need it back 1
source
share