6

I have the array with values:

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

The result will be:

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

It's ok.

But, if I add two elements to this array with a 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?

3
  • Syntax is important. So is looking at your error log when things dont work! Commented Feb 13, 2017 at 12:42
  • There is no any errors and syntax problems. Commented Feb 13, 2017 at 12:43
  • You will need to create a two dimensional array to store more than one value. Commented Feb 13, 2017 at 12:44

6 Answers 6

8

When php create the array, set Doctor in index 1 and Boss in index 2, but 2=>'Lynx' cause php overwrite index 2 and set Lynx in it.

You can set it after setted index or use index for it. For example like

$array2 = array("1"=>'Doctor', 2=>'Lynx', 'Boss', 'Lentin', 'Endless');
// or 
$array2 = array("1"=>'Doctor', 2=>'Boss', 3=>'Lynx', 'Lentin', 'Endless');
Sign up to request clarification or add additional context in comments.

Comments

5

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

Comments

3

Your issue is index keys

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

This is because, on index 1 it is already doctor, boss will be second, which will be replaced by Lynx which have same index of 2 where boss will be replaced.

I hope I am clear.

Comments

3

This is expected behaviour from php (see http://php.net/manual/en/language.types.array.php#example-57). In case you need all the values in the array and don't need to work with keys, I recommend to use array_push($array $value). Otherwise you should add all the values with their keys and remember that for PHP 1 and "1" and true are the same values so they will overwrite each other.

1 Comment

Which manual do you mean? In the link that I posted there is stated: "As all the keys in the above example are cast to 1, the value will be overwritten on every new element and the last assigned value "d" is the only one left over.". ==> In your example you firstly set "1"=>'Doctor' so the key is forced to 1. Next one is without key so only the value of the previous one is incremented to 2. You continue with setting 2=>'Lynx' but the key 2 is already used for Boss and that's why it is replaced with new value Lynx. In the end Boss is missing from the array altogether.
2

array() is a construct with dynamic arguments representing literal arrays. The assignment of the given values to the array structure is done sequentially i.e. one by one from left to right. In your example:

  1. Doctor is assigned to index 1.
  2. Boss is assigned to index 2.
  3. Lynx overwrites index 2.
  4. Lentin and Endless are assigned to index 3 and 4 respectively.

Comments

1

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

now come to your answer In php the array key can be string or integer and if you do not mention the key then default integer is set and the value of next array key is depending on the previous array integer key means

next array key = previous integer key + 1;

In PHP array the same key value will override by the same key

Now lets understand with your array2:

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

1) as you started your array with key "1" so the

so for 1st key value is [1] => 'Doctor'

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

now next key = previous integer key(that is 1) + 1 = 2;

2) for 2nd key value is [2] => 'BOSS'

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

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

Now the next key we have is [3]

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

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

now next key = previous integer key(that is 3) + 1 = 4;

5) for next value the key is [4] => 'Endless'

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

and that is why the final array is like below :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.