Home > Backend Development > PHP Problem > php remove associative array

php remove associative array

WBOY
Release: 2023-05-19 09:29:37
Original
585 people have browsed it

In PHP programming, array is a very useful and common data type that can be used to store various data. Associative arrays are a special array type that use string keys instead of numeric keys to access array elements. Sometimes, we need to delete certain elements from an associative array. This article will introduce how to remove an associative array in PHP.

1. Associative arrays in PHP

In PHP, an associative array is an array type that uses string keys to access array elements. It allows us to map strings onto values ​​to store and access data more easily. We can create an associative array using the following method:

$person = array(
    'name' => 'John',
    'age' => 25,
    'gender' => 'male'
);
Copy after login

In the above example, we used the => notation to associate the string key with the corresponding value, creating an An associative array named $person. We can use $person['name'] to access the first element in the array, 'John'. Similarly, we can use $person['age'] to access the second element in the array, 25.

2. Use the unset() function to delete array elements

If we want to delete certain elements from the above associative array, we can use the PHP built-in functionunset(). This function will delete the specified element from the array and reorder the keys and indexes in the array. Here is an example of using the unset() function to delete an associative array element:

$person = array(
    'name' => 'John',
    'age' => 25,
    'gender' => 'male'
);

unset($person['age']);

print_r($person);
Copy after login

In the above example, we used the unset() function to delete 'age 'element. The last lineprint_r()The function will display the deleted associative array contents:

Array
(
    [name] => John
    [gender] => male
)
Copy after login
Copy after login

You can see that the 'age' element has been deleted from the array.

3. Use the array_diff_key() function to delete elements with specified key names

Another way to delete associative array elements is to use the PHP built-in function array_diff_key(). This function will return an array containing elements that are not in the specified key list. The returned array can be reassigned to the original array to achieve the effect of deleting the specified key name. Here is an example of using the array_diff_key() function to delete an associative array element:

$person = array(
    'name' => 'John',
    'age' => 25,
    'gender' => 'male'
);

$person = array_diff_key($person, array('age' => ''));
print_r($person);
Copy after login

In the above example, we used the array_diff_key() function to delete 'age 'element. The first parameter of the function is the original array, and the second parameter is the array containing the key names that need to be deleted. The print_r() function on the last line will display the deleted associative array:

Array
(
    [name] => John
    [gender] => male
)
Copy after login
Copy after login

As you can see, the 'age' element has been deleted from the array.

4. Use the unset() function to delete multiple array elements

If you want to delete multiple specific array elements, we can pass it in the unset() function Enter multiple elements. Here is an example of using this method to delete associative array elements:

$person = array(
    'name' => 'John',
    'age' => 25,
    'gender' => 'male',
    'job' => 'developer',
    'hobby' => 'guitar'
);

unset($person['age'], $person['hobby']);

print_r($person);
Copy after login

In the above example, we used an unset() function to delete 'age' and 'hobby' element. The print_r() function on the last line will display the deleted associative array:

Array
(
    [name] => John
    [gender] => male
    [job] => developer
)
Copy after login

As you can see, the 'age' and 'hobby' elements have been deleted from the array.

5. Use the unset() function to delete the entire array

If you want to delete the entire associative array, you can use the unset() function and the variable name of the array. Here is an example of deleting an associative array using this method:

$person = array(
    'name' => 'John',
    'age' => 25,
    'gender' => 'male'
);

unset($person);

print_r($person);
Copy after login

In the above example, we used an unset() function to delete the entire associative array. The print_r() function on the last line will output an error message because the entire array has been deleted.

In this article, we introduced the method of using PHP built-in functions unset() and array_diff_key() to delete associative array elements. These methods are very simple and easy to understand, making it easier for you to work with associative arrays. During the development process, you need to pay attention to maintaining good code style and comments, and avoid deleting key data as much as possible.

The above is the detailed content of php remove associative array. 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