Задать вопрос

Вопрос по синтаксису php: почему элемент массива не отображается?

Здравствуйте!

Есть вот такой код на php:
<?php
$a = array(0.001 => 'b', .1 => 'c');

print_r($a);

?>


Вывод скрипта :
Array ( [0] => c )

Почему второй элемент не отображается?

С Уважением,
Алмик
  • Вопрос задан
  • 2388 просмотров
Подписаться 1 Оценить Комментировать
Помогут разобраться в теме Все курсы
  • Skillfactory
    Профессия Fullstack веб-разработчик на JavaScript и PHP
    20 месяцев
    Далее
  • Хекслет
    PHP-разработчик
    10 месяцев
    Далее
  • Нетология
    Веб-разработчик с нуля: профессия с выбором специализации
    14 месяцев
    Далее
Решения вопроса 1
Ключи массива могут быть либо инт либо стринг.
Все остальное приводится к ним.
В твоем случае приводится к инт = 0, первое значение перекрывается вторым.

Узнать шокирующие подробности:
php.net/manual/en/language.types.array.php

The key can either be an integer or a string. The value can be of any type.
Additionally the following key casts will occur:

Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
Null will be cast to the empty string, i.e. the key null will actually be stored under "".
Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы