Home  >  Article  >  Backend Development  >  What is the difference between js and php on arrays

What is the difference between js and php on arrays

PHPz
PHPzOriginal
2023-04-20 13:51:37412browse

With the development of the Internet, front-end technology and back-end technology have attracted more and more attention and attention. In front-end technology, JavaScript (JS for short) is a very popular programming language, while in back-end technology, PHP is one of the most widely used languages. Although there are many similarities in use between the two, there are still some differences in their array operations.

  1. Methods to declare an array

In JavaScript, you can use the following 3 methods to declare an array:

var arr1 = [1, 2, 3, 4];
var arr2 = new Array(1, 2, 3, 4);
var arr3 = new Array(4);

The first method is the most commonly used method, directly use [] to declare an array containing four elements 1, 2, 3, and 4. The second method creates a new array and adds elements 1,2,3,4 to it. The third method is to create an array with a length of 4, but the array has no elements, that is, each element in the array is undefined.

In PHP, the method of declaring an array is as follows:

$arr = array(1, 2, 3, 4);

If you need to create an empty array, you can use the following method:

$arr = array();
  1. Array access

In JavaScript, the elements in an array can be accessed using:

var arr = [1, 2, 3, 4];
console.log(arr[0]); //输出1

In PHP, the elements in an array can be accessed using:

$arr = array(1, 2, 3, 4);
echo $arr[0]; //输出1

It should be noted that in PHP you can also use associative arrays for access, that is, using a string as the key value of an element:

$arr = array('name' => '张三', 'age' => 20);
echo $arr['name']; //输出张三

You can also use associative arrays in JavaScript Access, but it actually just converts the associative array into a normal array for operation, so it is not commonly used.

  1. Getting the length of an array

In JavaScript, you can use the following two ways to get the length of an array:

var arr = [1, 2, 3, 4];
console.log(arr.length); //输出4
console.log(Object.keys(arr).length); //输出4

The first way is to use the array directly The length property obtains the length. The second method uses the keys method of the Object object to obtain the attribute name array, and then uses the length property to obtain the length.

In PHP, you can use the following method to obtain the length of an array:

$arr = array(1, 2, 3, 4);
echo count($arr); //输出4

Using the count method in PHP can easily obtain the length of an array.

  1. Array traversal

In JavaScript, you can use the following two methods to traverse an array:

var arr = [1, 2, 3, 4];
//第一种方式使用for循环
for (var i = 0; i < arr.length; i++) {
    console.log(arr[i]);
}
//第二种方式使用forEach方法
arr.forEach(function(value, index, array) {
    console.log(value);
});

The first method uses a for loop Traverse the array. The second way is to use the forEach method of the array, which can iterate each element in the array and pass the element to the callback function for processing.

In PHP, you can use the following two methods to traverse the array:

$arr = array(1, 2, 3, 4);
//第一种方式使用for循环
for ($i = 0; $i < count($arr); $i++) {
    echo $arr[$i];
}
//第二种方式使用foreach方法
foreach ($arr as $value) {
    echo $value;
}

The first method uses a for loop to traverse the array, and the second method uses the foreach statement to iterate each element in the array. elements and pass the elements to the variable $value for processing.

Summary:

  1. JavaScript and PHP arrays have some similarities, but there are still big differences in declaration, access, length acquisition and traversal.
  2. In JavaScript, arrays are usually declared as ordinary arrays, while in PHP, arrays are usually declared as associative arrays.
  3. Both JavaScript and PHP support the setting of array default values. JavaScript uses the fill() method and PHP uses the array_fill() function.
  4. Both JavaScript and PHP support array splicing, but the splicing methods are different. In JavaScript you can use the concat() method, and in PHP you can use the array_merge() function.

The above is the detailed content of What is the difference between js and php on arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn