// Manipulate on the two arrays with the same values both in PHP and JS.
// in PHP 
$array = [0, 1, 2];
$array2 = $array;
$array2[1] = 6;
array_push($array2, 3);
print_r ($array);
print_r($array2);
// $array = [0, 1, 2],    $array2 = [0, 6, 2, 3]
// In JS
let array = [0, 1, 2];
let array2 = array;
array2[1] = 6;
array2.push(3);
console.log(array);
console.log(array2);
// array = array2 = [0, 6, 2, 3]
// in JS It's the same array (since it's an object, it's the same reference), you need to create a copy to manipulate them separately // using slice() (or spread operator) in JS (which creates a new array with the elements at the first level copied over), like this:
let a = [0, 1, 2];
let b = a.slice();
b.push(3);
b[1] = 6;
console.log(a, b);
// and the result of 1st and 2nd arrays will not be same. like it wasn't in PHP
// P.s at that case will not change anything, because it's not reference already,
// we just took array's element, the primitive type (integer), not object or array.
let array = [0, 1, 2];
let value = array[1];
value = 10;
console.log(array);  // [0, 1, 2], will not change
console.log(value);    // 10
// but if there will be object, or array as an element in array and if we will modify on it, 
// than the main array will change the value as well.
const array = [{name: 'lela'}, {name: 'gela'}, {name: 'bela'}];
const object = array[0];
object.name = 'test';
console.log(array);
console.log(object);
by Luka Khitaridze
2 years ago
PHP
Array
0
Pro tip: use ```triple backticks around text``` to write in code fences