0

PHP array_keys() function

I can not understand, why it's output is 6? someone please help. $arr = [ "a" => "php", "b" => "npm", 4 => "foo", "pdo", "bar"]; $keys = array_keys($arr); echo $keys[4]; //output : 6

26th Nov 2019, 12:35 PM
A.H. Majumder đŸ‡§đŸ‡©
A.H. Majumder đŸ‡§đŸ‡© - avatar
2 Answers
+ 4
Because When you specify "4" as key for third element, next element will automatically take "5" as key if you won't specify it. And the last element will take "6" as you didn't specify it. array_keys() returns keys of all elements. $arr = [ "a" => "php", "b" => "npm", 4 => "foo", "pdo", "bar"]; $keys = array_keys($arr); //Here $keys = ["a", "b", 4, 5, 6] echo $keys[4];
26th Nov 2019, 12:43 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
Thank you so much. Your explanation was so easy.
26th Nov 2019, 12:55 PM
A.H. Majumder đŸ‡§đŸ‡©
A.H. Majumder đŸ‡§đŸ‡© - avatar