Assigning array to array php

Can I assign an array just by making it equal to another array?

I have an array $x with nonzero number of elements. I want to create another array ( $y ) which is equal to $x . Then I want to make some manipulations with $y without causing any changes to $x . Can I create $y in this way:

Gosh! This is too easy to test. the required time to write this questions is larger than writing a simple test to know what you want to know.

3 Answers 3

$a = array(0,1,2); $b = $a; $b[0] = 5; print_r($a); print_r($b); 
Array ( [0] => 0 [1] => 1 [2] => 2 ) Array ( [0] => 5 [1] => 1 [2] => 2 ) 

Array assignment always involves value copying. Use the reference operator to copy an array by reference.

No, a copy won’t change the original.

It would change it if you used a reference to the original array:

$a = array(1,2,3,4,5); $b = &$a; $b[2] = 'AAA'; print_r($a); 

Arrays are copied by value. There is a gotcha tho. If an element is a reference, the reference is copied but refers to the same object.

p = $p; > > // create an array of references $x = array( new testClass( 1 ), new testClass( 2 ) ); //make a copy $y = $x; print_r( array( $x, $y ) ); /* both arrays are the same as expected Array ( [0] => Array ( [0] => testClass Object ( [p] => 1 ) [1] => testClass Object ( [p] => 2 ) ) [1] => Array ( [0] => testClass Object ( [p] => 1 ) [1] => testClass Object ( [p] => 2 ) ) ) */ // change one array $x[0]->p = 3; print_r( array( $x, $y ) ); /* the arrays are still the same! Gotcha Array ( [0] => Array ( [0] => testClass Object ( [p] => 3 ) [1] => testClass Object ( [p] => 2 ) ) [1] => Array ( [0] => testClass Object ( [p] => 3 ) [1] => testClass Object ( [p] => 2 ) ) ) */ 

Источник

Читайте также:  Какая создать ссылку html

Adding an array to an array

DISC :- The array_merge() function merges one or more arrays into one array.

 'email@email.com1', 'm_field_id_9' => 'Name1'), array('email' => 'email@email.com2', 'm_field_id_9' => 'Name2') ]; $add = [array('email' => 'email@email.com3', 'm_field_id_9' => 'Name3')]; echo "
"; print_r(array_merge($array, $add)); 

Array push should have been fine:

 'foo@email.com', 'name' => 'Foo' ], [ 'email' => 'bar@email.com', 'name' => 'Bar' ], [ 'email' => 'baz@email.com', 'name' => 'Baz' ] ]; $item = [ 'email' => 'qux@email.com', 'name' => 'qux' ]; array_push($items, $item); var_export($items); 
array ( 0 => array ( 'email' => 'foo@email.com', 'name' => 'Foo', ), 1 => array ( 'email' => 'bar@email.com', 'name' => 'Bar', ), 2 => array ( 'email' => 'baz@email.com', 'name' => 'Baz', ), 3 => array ( 'email' => 'qux@email.com', 'name' => 'qux', ), ) 

It's a useful function if you have more than one element to add.

However it's easier to just append to the array:

Ideal in your Case is to simply use the Square Brackets and indicate the 3rd Slot @index Nr. 2 . because you are dealing with a multi-dimensionally array here.

 $add = array('email' => 'email@email.com2', 'm_field_id_9' => 'Name2'); $mainArray = [ [ 'email' => 'email@email.com', 'm_field_id_9' => 'Name', ], [ 'email' => 'email@email.com', 'm_field_id_9' => 'Name', ], [ 'email' => 'email@email.com', 'm_field_id_9' => 'Name', ], ]; // SIMPLY USE THE SQUARE BRACKETS HERE FOR THIS. // TO OVERWRITE THE INDEX LOCATION#3 (3RD ARRAY) GO: $mainArray[2] = $add; // TO JUST APPEND TO THE END OF THE LAST ITEM THUS CREATING A 4TH SUB-ARRAY, GO: $mainArray[] = $add; 

This question is in a collective: a subcommunity defined by tags with relevant content and experts.

Источник

Assigning array to array php

// Before php 5.4
$array = array(1,2,3);

// since php 5.4 , short syntax
$array = [1,2,3];

// I recommend using the short syntax if you have php version >= 5.4

Used to creating arrays like this in Perl?

Looks like we need the range() function in PHP:

$array = array_merge (array( 'All' ), range ( 'A' , 'Z' ));
?>

You don't need to array_merge if it's just one range:

There is another kind of array (php>= 5.3.0) produced by

$array = new SplFixedArray(5);

Standard arrays, as documented here, are marvellously flexible and, due to the underlying hashtable, extremely fast for certain kinds of lookup operation.

Supposing a large string-keyed array

$arr=['string1'=>$data1, 'string2'=>$data2 etc. ]

when getting the keyed data with

php does *not* have to search through the array comparing each key string to the given key ('string1') one by one, which could take a long time with a large array. Instead the hashtable means that php takes the given key string and computes from it the memory location of the keyed data, and then instantly retrieves the data. Marvellous! And so quick. And no need to know anything about hashtables as it's all hidden away.

However, there is a lot of overhead in that. It uses lots of memory, as hashtables tend to (also nearly doubling on a 64bit server), and should be significantly slower for integer keyed arrays than old-fashioned (non-hashtable) integer-keyed arrays. For that see more on SplFixedArray :

Unlike a standard php (hashtabled) array, if you lookup by integer then the integer itself denotes the memory location of the data, no hashtable computation on the integer key needed. This is much quicker. It's also quicker to build the array compared to the complex operations needed for hashtables. And it uses a lot less memory as there is no hashtable data structure. This is really an optimisation decision, but in some cases of large integer keyed arrays it may significantly reduce server memory and increase performance (including the avoiding of expensive memory deallocation of hashtable arrays at the exiting of the script).

When creating arrays , if we have an element with the same value as another element from the same array, we would expect PHP instead of creating new zval container to increase the refcount and point the duplicate symbol to the same zval. This is true except for value type integer.
Example:

$arr = ['bebe' => 'Bob', 'age' => 23, 'too' => 23 ];
xdebug_debug_zval( 'arr' );

(refcount=2, is_ref=0)
array (size=3)
'bebe' => (refcount=1, is_ref=0)string 'Bob' (length=3)
'age' => (refcount=0, is_ref=0)int 23
'too' => (refcount=0, is_ref=0)int 23

but :
$arr = ['bebe' => 'Bob', 'age' => 23, 'too' => '23' ];
xdebug_debug_zval( 'arr' );

(refcount=2, is_ref=0)
array (size=3)
'bebe' => (refcount=1, is_ref=0)string 'Bob' (length=3)
'age' => (refcount=0, is_ref=0)int 23
'too' => (refcount=1, is_ref=0)string '23' (length=2)
or :

$arr = ['bebe' => 'Bob', 'age' => [1,2], 'too' => [1,2] ];
xdebug_debug_zval( 'arr' );

(refcount=2, is_ref=0)
array (size=3)
'bebe' => (refcount=1, is_ref=0)string 'Bob' (length=3)
'age' => (refcount=2, is_ref=0)
array (size=2)
0 => (refcount=0, is_ref=0)int 1
1 => (refcount=0, is_ref=0)int 2
'too' => (refcount=2, is_ref=0)
array (size=2)
0 => (refcount=0, is_ref=0)int 1
1 => (refcount=0, is_ref=0)int 2

This function makes (assoc.) array creation much easier:

function arr (. $array )< return $array ; >
?>

It allows for short syntax like:

$arr = arr ( x : 1 , y : 2 , z : 3 );
?>

Instead of:

$arr = [ "x" => 1 , "y" => 2 , "z" => 3 ];
// or
$arr2 = array( "x" => 1 , "y" => 2 , "z" => 3 );
?>

Sadly PHP 8.2 doesn't support this named arguments in the "array" function/language construct.

Источник

Оцените статью