0

What is the difference in the following array notations: $arr[$key] = $value and $arr[] = $value, which is a better way ?

function test(){
$key = 0;
$a = array(1,2,3,4,5);
foreach($a as $value){
   $a[$key] = $value;
   $key++;
}
print_r($a);
}

versus

function test(){
$a = array(1,2,3,4,5);
foreach($a as $value){
 $a[] = $value;
}
print_r($a);
}
3
  • 3
    One is not better than the other. Commented Feb 25, 2011 at 16:40
  • By better, i mean in terms of code efficiency / standard Commented Feb 25, 2011 at 16:40
  • None of them is better they have different usages, you just happen to use a scenario that outlines both as an 'insert', one defines the key, the other just adds to the array. Commented Feb 25, 2011 at 16:43

4 Answers 4

5

They are different.

$a[] = 'foo';

Adds an element to the end of the array, creating a new key for it (and increasing the overall size of the array). This is the same as array_push($array, 'foo');

$key = 0;
$a[$key] = 'foo';

Sets the 0 element of the array to foo, it overwrites the value in that location... The overall size of the array stays the same... This is the same as $array = array_slice($array, 0, 1, 'foo'); (but don't use that syntax)...

In your specific case, they are doing 2 different things. The first test function will result in an array array(1,2,3,4,5), whereas the second one will result in array(1,2,3,4,5,1,2,3,4,5). [] always adds new elemements to the end.... [$key] always sets....

Sign up to request clarification or add additional context in comments.

6 Comments

Well in his case, they work the same because he increments $key to the last index every time.
But in terms of simply creating an array $temp and filling it with values, $temp[] = 'foo' will automatically increment right unlink $temp[$key] where we have to mention $key++
@user550265 Of course, that's why I still would go with $a[] = $val
In the examples should I'd skip the foreach completely and just use the $a = array(1,2,3,4,5); line which (seemingly) gives you the array that you actually want in the first place.
It all depends on what's trying to be done. We can see what the code is doing, but what is it supposed to be doing?
|
0

$arr[$key] = $value sets a specific key to a specific value.

$arr[] = $value adds a value on to the end of the array.

Neither is "better". They serve completely different roles. This is like comparing writing and drawing. You use a pen for both of them, but which you use depends on the circumstance.

Comments

0

One ($a[$key] = $value) youare specifying the $key where PHP will override that array entry if you use the same key twice.

The other ($a[] = $value) PHP is handling the keys itself and just adding the array entry using the next available key.

The whole thing that you are doing is a bit redundant though, as in the first example you are trying to loop through the array setting its values to itself. In the second example you are duplicating the array.

Comments

0

If you want to append an element to the array I would use

$arr[] = $value;

It is simpler and you get all the information on that line and don't have to know what $key is.

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.