Why does PHP lose the value of an array element?

I have an array with values:

$array1 = array('Boss', 'Lentin', 'Endless'); print_r ($array); 

The result will be:

 Array ( [0] => Boss [1] => Lentin [2] => Endless 

This is normal.

But if I add two elements to this array using the keys, the Boss element will be lost.

 $array2 = array("1"=>'Doctor','Boss', 2=>'Lynx', 'Lentin', 'Endless'); print_r ($array2); 

The result will be:

 Array ( [1] => Doctor [2] => Lynx [3] => Lentin [4] => Endless ) //Where is "BOSS"??? 

Why?

+6
source share
6 answers

When php creates an array, set Doctor at index 1 and Boss at index 2, but 2=>'Lynx' force php to rewrite index 2 and install Lynx in it.

You can install it after the installed index or use the index for it. For example, for example

 $array2 = array("1"=>'Doctor', 2=>'Lynx', 'Boss', 'Lentin', 'Endless'); // or $array2 = array("1"=>'Doctor', 2=>'Boss', 3=>'Lynx', 'Lentin', 'Endless'); 
+8
source

When the $ array is created, the “Boss” will first be stored in index 2 (Array([2] =>'Boss') , which will later be overwritten by "Lynx"

+5
source

Your problem is the index keys

 $array2 = array("1"=>'Doctor','Boss', 2=>'Lynx', 'Lentin', 'Endless'); print_r ($array2); 

This is because in index 1 it is already a doctor, the boss will be the second to be replaced by Lynx, which has the same index 2, where the boss will be replaced.

I hope I get it.

+3
source

This is the expected behavior from php (see http://php.net/manual/en/language.types.array.php#example-57 ). If you need all the values ​​in an array and you do not need to work with keys, I recommend using array_push($array $value) . Otherwise, you must add all the values ​​using your keys and remember that for PHP 1 and "1" and true will be the same values ​​so that they overwrite each other.

+3
source

array () is a construct with dynamic arguments representing literal arrays. Assignment of setpoints to the array structure is performed sequentially , that is, one by one from left to right. In your example:

  • Doctor assigned index 1.
  • Boss assigned to index 2.
  • Lynx overwrites index 2.
  • Lentin and Endless are assigned indexes 3 and 4, respectively.
+2
source

hey @Weltkind first of all I suggest you read " http://php.net/manual/en/language.types.array.php ",

will now come to your answer. In php, the array key can be a string or an integer, and if you do not specify a key, then a default integer is specified, and the value of the next array key depends on the previous element of the integer key

next array key = previous integer key + 1;

In a PHP array, the same key value will be overridden by the same key

Now let's deal with your array2:

 <?php $array2 = array("1"=>'Doctor','Boss', 2=>'Lynx', 'Lentin', 'Endless'); 

1) when you started your array with the "1" key so that

therefore, for the 1st key value [1] => "Doctor"

current array like: array ([1] => 'Doctor')

now the next key = previous integer key (i.e. 1) + 1 = 2;

2) for the second key value [2] => 'BOSS'

current array like: array ([1] => 'Doctor', [2] => 'BOSS')

3) next key = previous integer key (that is 2) + 1 = 3 it will transfer to the next key, but the next key is [2] => "Lynx", as you mentioned key [2] the value will be overridden by the value "BOSS "on" Lynx "; current array like: array ([1] => 'Doctor', [2] => 'Lynx')

Now we have the next key [3]

4) for the next value, the key [3] => 'Lentin'

current array like: array ([1] => "Doctor", [2] => "Lynx", [3] => 'Lentin');

now next key = previous integer key (i.e. 3) + 1 = 4;

5) for the next value, the key [4] => "Infinite"

current array like: array ([1] => "Doctor", [2] => "Lynx", [3] => "Lentin", [4] => "Infinite");

and so the last array looks like this:

 array( [1] => 'Doctor', [2] => 'Lynx', [3] => 'Lentin', [4] => 'Endless' ); 
+1
source

Source: https://habr.com/ru/post/1014939/


All Articles