Home > Backend Development > PHP Problem > What is the new way to write php arrays?

What is the new way to write php arrays?

PHPz
Release: 2023-04-23 13:46:51
Original
507 people have browsed it

PHP is a commonly used server-side scripting language that can quickly generate dynamic web content. One of the most commonly used data structures in PHP is arrays, but many developers may only know one of the fixed writing methods, and do not know that there are many new writing methods and techniques. This article will introduce the new writing methods of PHP arrays.

1. Add elements

The most basic thing is to add elements to an array. Usually, $array[] = $value is used, which is very easy. understand. However, you can also add values ​​to the end of an array by using the array_push() function.

$array = array();
array_push($array, 'apple', 'banana', 'cherry');
print_r($array); // Array ( [0] => apple [1] => banana [2] => cherry )
Copy after login

You can also add elements in batches using key names, so that one array can be merged into another array.

$array1 = array('a', 'b', 'c');
$array2 = array('d' => 'dog', 'e' => 'elephant');
$result = $array1 + $array2;
print_r($result); // Array ( [0] => a [1] => b [2] => c [d] => dog [e] => elephant )
Copy after login

2. Delete elements

You can usually use the unset() function to delete an array element.

$array = array('apple', 'banana', 'cherry');
unset($array[1]);
print_r($array); // Array ( [0] => apple [2] => cherry )
Copy after login

You can also use the array_splice() function to delete specified elements from the array. It can also return the removed element after removing it.

$array = array('apple', 'banana', 'cherry');
$res = array_splice($array, 1, 1); // 在第1个位置开始,删除1个元素
print_r($array); // Array ( [0] => apple [1] => cherry )
print_r($res); // Array ( [0] => banana )
Copy after login

3. Copy an array

You can usually use the writing method $copy = $array to copy an array, but this writing method only copies the reference, that is, the variable name. pointer. This way, when one of the arrays is modified, the other array will also change.

The real way to copy is to use the $copy = array_slice($array) function, so that all elements can be copied.

$array = array('apple', 'banana', 'cherry');
$copy = array_slice($array);
print_r($copy); // Array ( [0] => apple [1] => banana [2] => cherry )
Copy after login

4. Obtain elements

When obtaining array elements, the writing method $element = $array[$index] is usually used. However, if the element does not exist, an error will occur. You can use the isset() function to check whether an element exists.

$array = array('apple', 'banana', 'cherry');
if (isset($array[1])) {
    $element = $array[1];
    echo $element; // banana
} else {
    echo 'not found';
}
Copy after login

You can also use the array_key_exists() function to check whether a key exists.

$array = array('d' => 'dog', 'e' => 'elephant');
if (array_key_exists('e', $array)) {
    $value = $array['e'];
    echo $value; // elephant
} else {
    echo 'not found';
}
Copy after login

5. Sorting

PHP has many sorting functions, including sort(), rsort(), asort(), arsort(), ksort(), krsort(), etc. are also relatively simple to use. However, if you want to sort based on a key in the array, you need to use the usort() function and provide a custom sort function.

function cmp($a, $b) {
    if ($a['age'] == $b['age']) {
        return 0;
    }
    return ($a['age'] < $b[&#39;age&#39;]) ? -1 : 1;
}
$array = array(
    array(&#39;name&#39; => 'John', 'age' => 23),
    array('name' => 'Mary', 'age' => 25),
    array('name' => 'Peter', 'age' => 21)
);
usort($array, 'cmp');
print_r($array); // Array ( [0] => Array ( [name] => Peter [age] => 21 ) [1] => Array ( [name] => John [age] => 23 ) [2] => Array ( [name] => Mary [age] => 25 ) )
Copy after login

6. Traversing

Traversing an array is one of the most important tasks in PHP development. You can use foreach() to loop through the array.

$array = array('apple', 'banana', 'cherry');
foreach ($array as $value) {
    echo $value . ' ';
}
// apple banana cherry
Copy after login

You can also use foreach() to traverse an associative array.

$array = array('d' => 'dog', 'e' => 'elephant');
foreach ($array as $key => $value) {
    echo $key . ': ' . $value . ' ';
}
// d: dog e: elephant
Copy after login

Some additions

When using PHP arrays, you need to pay attention to the following issues:

  1. The array in PHP is actually an ordered map, and Not a simple list.
  2. The subscripts of the array can be integers and strings, and the keys and values ​​can be any PHP data type.
  3. Arrays in PHP are dynamic and elements can be added, deleted and modified at any time. This makes them very easy to use, but can sometimes lead to unintended consequences.
  4. PHP’s arrays have many other useful functions, such as array_merge(), array_filter(), array_reduce(), etc.

Conclusion

In PHP, arrays are a very useful data structure that can enable many interesting programming techniques. This article introduces some new array writing methods and techniques, allowing developers to better utilize arrays in PHP to process data. I hope this article can be helpful to PHP developers.

The above is the detailed content of What is the new way to write php arrays?. 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