I have an PHP Array with Object with below sample:
Array
(
[0] => Array
(
[menuID] => 1
[menuName] => AA
[menuURL] =>
[subMenu] => Array
(
[0] => Array
(
[subMenuID] => 1
[subMenuName] => Sub 1
[subMenuURL] => ab
)
[1] => Array
(
[subMenuID] => 2
[subMenuName] => Sub 2
[subMenuURL] => ac
)
)
)
[1] => Array
(
[menuID] => 2
[menuName] => AB
[menuURL] =>
[subMenu] =>
)
)
Now I need to count and access array in subMenu
but got strange value there.
I’m using this code:
foreach ($headerMenu as $list) {
//print_r($list['menuName']);
echo "<pre>";
print_r(count($list['subMenu']));
}
It show me result 2
and 1
. As you can see on the above sample array, it should show 2
for menu ID 1 and 0
for menu ID 2.
Is there any way how to solve it?
2
Answers
I’m not sure about the error, but i just change the syntax, and try it in wamp, and it did print 2 and 0.
better to use the
is_array()
and check ifsubMenu
is an array or not before counting it!