What is the array representation method in php

PHPz
Release: 2023-04-19 10:40:14
Original
632 people have browsed it

PHP is a scripting language widely used in Web development. One of the most basic and commonly used data structures is arrays. In PHP, arrays can contain any type of data, such as numbers, strings, booleans, objects, etc. In this article, we will clearly explain the representation methods used by PHP arrays and their related operations.

  1. Creation and initialization of arrays

There are two ways to create arrays in PHP: one is to use the array() function to create it, and the other is to use Square brackets [] are used to create it. The effect of both is the same. Let’s take a look at the specific implementation.

Use the array() function to declare:

$names = array('Bob', 'Tom', 'Jerry');
Copy after login

Use square brackets [] to declare:

$ages = ['Bob'=>25, 'Tom'=>30, 'Jerry'=>35];
Copy after login

The above two different declaration methods can successfully create arrays. The difference The reason is: the former uses array() to reference the function when declaring, while the latter directly uses square brackets [] to declare. The effects of the two methods are exactly the same. Please choose which method to use according to your own preferences.

  1. Array access and output

The method of accessing array elements is the same as in other programming languages, using square brackets [], in which the corresponding values ​​of the array elements are written in the square brackets The corresponding element can be accessed by key name or serial number. For example:

$names = array('Bob', 'Tom', 'Jerry');
echo $names[0];     //输出Bob
echo $names[2];     //输出Jerry

$ages = ['Bob'=>25, 'Tom'=>30, 'Jerry'=>35];
echo $ages['Tom'];  //输出30
Copy after login

In addition to using echo for output, PHP can also use the print_r() function or var_dump() function for array output. Among them, the output result of the print_r() function is easier to read, and the var_dump() function can not only output the contents of the array, but also output detailed information such as the data type and length of the element. For example:

$names = array('Bob', 'Tom', 'Jerry');
print_r($names);    //输出:Array ( [0] => Bob [1] => Tom [2] => Jerry )
var_dump($names);   //输出:array(3) { [0]=> string(3) "Bob" [1]=> string(3) "Tom" [2]=> string(5) "Jerry" }
Copy after login
  1. Traversal of arrays

Traversing arrays can be operated through the for loop and foreach statement provided by PHP. Let’s take a look at their implementation methods below.

Use a for loop to traverse the array:

$names = array('Bob', 'Tom', 'Jerry');
$arrayLength = count($names);   //获取数组长度
for($i = 0; $i < $arrayLength; $i++){
    echo $names[$i] . &#39; &#39;;      //遍历输出数组元素 
}
Copy after login

Use the foreach statement to traverse the array:

$ages = [&#39;Bob&#39;=>25, 'Tom'=>30, 'Jerry'=>35];
foreach ($ages as $key => $value){
    echo $key . ' is ' . $value . ' years old.';     //遍历输出数组元素和键名
}
Copy after login

Use the list() function to traverse the array:

$nameArr = ['Bob', 'Tom', 'Jerry'];
list($name1, $name2, $name3) = $nameArr;
echo $name1 . ', ' . $name2 . ', ' . $name3;   //遍历输出数组元素
Copy after login
  1. Array operations

PHP provides a series of array operation functions. Here we list some common operation methods.

Add elements: Use the array_push() or array_unshift() function to add new elements to the end or beginning of the array.

$names = array('Bob', 'Tom', 'Jerry');
array_push($names, 'John');    //添加新元素到末尾
array_unshift($names, 'Lucy'); //添加新元素到开头
print_r($names);    //输出:Array ( [0] => Lucy [1] => Bob [2] => Tom [3] => Jerry [4] => John )
Copy after login

Delete elements: Use the array_pop() or array_shift() function to delete elements at the end or beginning of the array.

$names = array('Bob', 'Tom', 'Jerry');
array_pop($names);    //删除末尾元素
array_shift($names);  //删除开头元素
print_r($names);    //输出:Array ( [0] => Tom )
Copy after login

Merge arrays: Use the array_merge() function to merge two arrays into a new array.

$names1 = array('Bob', 'Tom');
$names2 = array('Jerry', 'Lucy');
$names = array_merge($names1, $names2);   //将两个数组合并为一个新数组
print_r($names);     //输出:Array ( [0] => Bob [1] => Tom [2] => Jerry [3] => Lucy )
Copy after login

Find the intersection and difference: Use the array_intersect() function to find the intersection of two arrays, and use the array_diff() function to find the difference of two arrays.

$nums1 = [1, 2, 3, 4, 5];
$nums2 = [3, 4, 5, 6, 7];
$intersect = array_intersect($nums1, $nums2);     //求交集
print_r($intersect);   //输出:Array ( [2] => 3 [3] => 4 [4] => 5 )

$diff = array_diff($nums1, $nums2);      //求差集
print_r($diff);    //输出:Array ( [0] => 1 [1] => 2 )
Copy after login
  1. Sorting of Arrays

PHP has many built-in array sorting functions that can sort arrays in ascending or descending order. Here we list several commonly used operations. It is worth noting that the sorting function will change the order of the elements in the original array.

Sort in ascending order: Use the sort() function to sort the array elements in ascending order.

$nums = [6, 4, 2, 8, 1];
sort($nums);
print_r($nums);     //输出:Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 6 [4] => 8 )
Copy after login

Sort in descending order: Use the rsort() function to sort the array elements in descending order.

$nums = [6, 4, 2, 8, 1];
rsort($nums);
print_r($nums);     //输出:Array ( [0] => 8 [1] => 6 [2] => 4 [3] => 2 [4] => 1 )
Copy after login

Sort by associated key value: use the asort() function to sort the array elements in ascending order (but will not affect the key value), use the arsort() function to sort the array elements in descending order (but will not affect the key value) value).

$ages = ['Bob'=>25, 'Tom'=>30, 'Jerry'=>35];
asort($ages);
print_r($ages);     //输出:Array ( [Bob] => 25 [Tom] => 30 [Jerry] => 35 )

arsort($ages);
print_r($ages);     //输出:Array ( [Jerry] => 35 [Tom] => 30 [Bob] => 25 )
Copy after login

Sort by associated key name: Use the ksort() function to sort the array elements in ascending order by key name, and use the krsort() function to sort the array elements in descending order by key name.

$ages = ['Bob'=>25, 'Tom'=>30, 'Jerry'=>35];
ksort($ages);
print_r($ages);     //输出:Array ( [Bob] => 25 [Jerry] => 35 [Tom] => 30 )

krsort($ages);
print_r($ages);     //输出:Array ( [Tom] => 30 [Jerry] => 35 [Bob] => 25 )
Copy after login

All the above are some common representation methods of PHP arrays and related operations. We can see that PHP array is a very flexible and convenient data structure, and PHP also provides a wealth of array operation technologies, making it easier and more efficient for us to develop when using arrays.

The above is the detailed content of What is the array representation method in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template