
Originally Posted by
ggalan
i am adding to an existing array and it seems to work like this
Code:
$arr[] .= 'a';
$arr[] .= 'b';
$arr[] .= 'c';
but if i wanted it written in 1 line like this, what is the correct way?
this doesnt seem to work
First: you shouldn't be using the concatenation assignment ( .= ).
Once that's corrected, both methods will work, but will provide different results:
PHP Code:
<?php
$array1[] = 'a';
$array1[] = 'b';
$array1[] = 'c';
var_dump( $array1 );
/* output:
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
BTW, this:
$arr = array();
array_push($arr, 'a','b','c' );
has _exactly_ the same result as the method above (but is less efficient)
*/
$array2[] = array( 'a','b','c' );
var_dump( $array2 );
/* output:
array(1) {
[0]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
*/
I suspect what you want, however, is a simple array construct:
PHP Code:
<?php
$array = array( 'a','b','c' );
var_dump( $array );
/* output:
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
*/
Bookmarks