I have this kind of array:
$a = array(
'one' => 'one',
'0' => '0',
'two' => 'two',
'three' => 'three',
'four'
);
as you can see it is an associative array BUT not all the keys have the value (take a look at the last).
My question is, how can i loop this kind of array to get key(if exists) and the respective value?
Thank you!
The string
'four'in your example is not a key but a value. The corresponding key will be1. This happens because PHP converts the string key'0'to numeric key0and for the value'four'it uses the next numeric key which will be1.Reference:
To have a key with no value you can use
NULLas the value:Similarly to have an empty key use
nullas key:And to loop over such an array you can use a
foreachloop. To detect if a key/value is null or not you can use the isset function.