Home  >  Article  >  Backend Development  >  How to remove elements from an array in php

How to remove elements from an array in php

PHPz
PHPzOriginal
2023-04-18 09:06:14562browse

PHP is a widely used server-side scripting language that is often used to develop web applications. In PHP, an array is a powerful data type that can hold multiple values ​​and be accessed through indexing. In this article, we will introduce how to remove the elements of an array in PHP.

Basic definition of array

In PHP, arrays can be defined using array() or []. Here are some examples:

// 定义一个空数组
$empty_array = array();
// 定义一个数字索引数组
$number_array = array(1, 2, 3, 4, 5);
// 定义一个关联数组
$assoc_array = array('name' => 'John', 'age' => 25, 'gender' => 'male');
// 使用[]定义关联数组
$assoc_array_2 = ['name' => 'Mary', 'age' => 20, 'gender' => 'female'];

Getting array elements by numeric index

You can get elements from an array using numeric indexing. Each element has a unique numeric index. For example, if you want to get the first element of the above $number_array array, you can use the following code:

echo $number_array[0];
// 输出: 1

Note that the array index starts from 0 and not 1.

We can also use a for loop to traverse the numeric index array:

for ($i = 0; $i < count($number_array); $i++) {
    echo $number_array[$i] . " ";
}
// 输出: 1 2 3 4 5

Retrieve array elements through associated keys

We can also use associated keys to obtain elements from the array. Each element has a unique associated key, usually a string or number. For example, if you want to get the 'name' key value in the above $assoc_array array, you can use the following code:

echo $assoc_array[&#39;name&#39;];
// 输出: John

We can also use a foreach loop to traverse the associative array:

foreach ($assoc_array as $key => $value) {
    echo $key . ": " . $value . "<br>";
}
// 输出:
// name: John
// age: 25
// gender: male

In the above code , $key represents the associated key, and $value represents the value corresponding to the associated key.

How to take out the elements of a multi-dimensional array

PHP allows the use of multi-dimensional arrays, that is, the array can contain another array. For example, here is an example of a two-dimensional array:

$multi_array = array(
    array('apple', 'banana', 'orange'),
    array('red', 'green', 'blue'),
    array('one' => 1, 'two' => 2, 'three' => 3)
);

You can use two numeric indexes to obtain elements in a multi-dimensional array. The first numeric index represents the index of the outer array, and the second numeric index represents the index of the outer array. Represents the index of the inner array. For example, if you want to get the 'banana' element in the $multi_array array, you can use the following code:

echo $multi_array[0][1];
// 输出: banana

When using a foreach loop to traverse a multi-dimensional associative array, you can use two variables to represent the association key and the associative array. For example, if you want to traverse all elements in the above $multi_array array, you can use the following code:

foreach ($multi_array as $key1 => $array1) {
    foreach ($array1 as $key2 => $value) {
        echo "multi_array[" . $key1 . "][" . $key2 . "] = " . $value . "<br>";
    }
}

// 输出:
// multi_array[0][0] = apple
// multi_array[0][1] = banana
// multi_array[0][2] = orange
// multi_array[1][0] = red
// multi_array[1][1] = green
// multi_array[1][2] = blue
// multi_array[2][one] = 1
// multi_array[2][two] = 2
// multi_array[2][three] = 3

In the above code, $key1 represents the associated key of the outer array, $array1 represents the inner array, $key2 represents the associated key of the inner array, and $value represents the value in the inner array.

Summary

PHP’s array is a very powerful data type that can accommodate multiple values ​​and be accessed through indexes. In PHP, you can get elements from an array using a numeric index, an associative key, or a combination of both. For multidimensional arrays, you can use two numeric indexes or nested foreach loops to get elements. I hope this article can help you better understand how to use arrays in PHP.

The above is the detailed content of How to remove elements from an array in php. 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