Answer the question
In order to leave comments, you need to log in
PHP: Do I need to create an array before populating it?
Do I need to initialize an array before adding elements to it?
In the past, I always meticulously checked for the presence of all parent branches of an array, but recently I use only one isset. Now I'm wondering whether it's worth creating parent subarrays or immediately entering the final element (and php will create all intermediate links automatically). How to do it right?
Reading
Was:
echo isset($a['a']) && isset($a['a']['b']) && isset($a['a']['b']['c']) ? 'exists' : 'error';
echo isset($a['a']['b']['c']) ? 'exists' : 'error';
if (!isset($_SESSION['voted']) || !is_array($_SESSION['voted']))
{
$_SESSION['voted'] = array();
}
$_SESSION['voted'][$voteId] = time();
$_SESSION['voted'][$voteId] = time();
Answer the question
In order to leave comments, you need to log in
en2.php.net/manual/en/language.types.array.php
The documentation clearly says that it is possible.
$arr[key] = value;
$arr[] = value;
// key may be an integer or string
// value may be any value of any type
//If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array
Warnings may appear at the highest level, however, this approach is acceptable.
If $a['a']['b']['c'] exists, then $a['a']['b'] must exist by definition.
So checking parent branches is redundant in this case.
C isset
is absolutely correct. But I wouldn't call it "reading" because. isset
is a special language construct. If you try to ask the index of a non-array, then:
unset($a);
echo $a['a']['b']['c']; // будет warning
echo $a['a']; // тоже будет warning
isset($a['a']['b']['c']); // не будет warning'а
<?php
unset($a);
$a['a'] = 'a'; // это ок
$a['b'] = 1; // это ок
$a['a'][0] = 'b'; // перезапишет первый символ строки!!!
$a['b'][0] = 'b'; // будет notice, про обращение к скаляру, как к массиву
unset($a);
$a['a']['b']['c']['d'] = 'str'; // тоже ок
$с = new SomeClass;
$с[0] = 'c'; // Если класс имплементил ArrayAccess интерфейс, то вызовется метод offsetGet,
// иначе Fatal error
It is impossible to check with isset. for a null value, it returns false. See the doc:
$a = array ('test' => 1, 'hello' => NULL);
var_dump(isset($a['test'])); // TRUE
var_dump(isset($a['hello'])); // FALSE
To check if an array key exists, use array_key_exists():
var_dump(array_key_exists('hello', $a)); // TRUE
en.php.net/manual/en/function.isset.php
I think that you need to check if you do not want to overwrite an existing value, i.e.
if (!isset($_SESSION['voted'][$voteId])) {
// Ещё не голосовали, учитываем голос
$_SESSION['voted'][$voteId] = time();
}
I think it's enough to check the end element of the array. And if something confuses you, turn on the PHP error log with the maximum logging level: if there are not even warnings, then everything is ok.
there is the most super-high level - there are no notices, you can use it, version 5.3.6, what will happen next is unknown.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question