Skip to content
Advertisement

How to store a zero in an array in php

I am trying to store the value 0 in an associative array to later use it as a key to another array.

However I am not able to retrieve 0 with the code below.

What happens is that in the while loop they ‘subject’ key is completely ignored when the contents are 0.

Any other value is fine.

How can I loop over the associative array and get the 0 value?

And what is the reason for this behaviour?

JavaScript

Advertisement

Answer

Because 0 evaluates loosely to false and the while terminates. Test explicitly:

JavaScript

However, why are you not using a foreach?

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement