Home  >  Article  >  Backend Development  >  How to delete elements from multidimensional array in PHP

How to delete elements from multidimensional array in PHP

PHPz
PHPzOriginal
2023-04-19 14:11:41627browse

In PHP development, using arrays is an essential part. For multi-dimensional arrays, especially when used in complex applications, the operation may be somewhat complicated.

This article will introduce how to delete elements from multi-dimensional arrays in PHP.

  1. Deleting one-dimensional array elements

In PHP, deleting elements in a one-dimensional array is very simple. You can use the unset() function to delete specified elements.

Sample code:

$arr = array('apple', 'banana', 'orange', 'grape');
unset($arr[2]);
print_r($arr);

Output:

Array
(
    [0] => apple
    [1] => banana
    [3] => grape
)

In the above example, we deleted the third element "orange", and only the others are left in the output result three elements.

  1. Delete two-dimensional array elements

For two-dimensional arrays, we need to first find the location of the element that needs to be deleted. We can then delete the element using the unset() function.

Sample code:

$arr = array(
    array('name' => 'Tom', 'age' => 18),
    array('name' => 'Jerry', 'age' => 19),
    array('name' => 'Mike', 'age' => 20),
);
unset($arr[1]);
print_r($arr);

Output:

Array
(
    [0] => Array
        (
            [name] => Tom
            [age] => 18
        )

    [2] => Array
        (
            [name] => Mike
            [age] => 20
        )

)

In the above example, we have removed the second element that contains "name"=>"Jerry" of that array. Only the other two elements are left in the output.

  1. Delete associative array elements

For associative arrays, we can delete elements by key name. Likewise, just use the unset() function.

Sample code:

$arr = array(
    'name' => 'Tom',
    'age' => 18,
    'gender' => 'male',
);
unset($arr['age']);
print_r($arr);

Output:

Array
(
    [name] => Tom
    [gender] => male
)

In the above example, we deleted the element with the key name "age". Only the other two elements are left in the output.

  1. Delete multi-dimensional array elements

In a multi-dimensional array, the operation of deleting an element is to first locate the location of the element, and then use the unset() function to delete the element. element.

Sample code:

$arr = array(
    'A' => array('a', 'b', 'c'),
    'B' => array('d', 'e', 'f'),
    'C' => array('g', 'h', 'i'),
);
unset($arr['B'][1]);
print_r($arr);

Output:

Array
(
    [A] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [B] => Array
        (
            [0] => d
            [2] => f
        )

    [C] => Array
        (
            [0] => g
            [1] => h
            [2] => i
        )

)

In the above example, we removed $arr'B', which is the second one in the array "B" Element "e". In the output result, only other elements are left.

It should be noted that in PHP, deleting elements may affect the index value of the array. If you want to keep the index values ​​continuous and starting from 0, you can re-index the array using the array_values() function.

Sample code:

$arr = array('apple', 'banana', 'orange', 'grape');
unset($arr[2]);
$arr = array_values($arr);
print_r($arr);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => grape
)

In the above example, we have removed the "orange" element again and re-indexed the array using the array_values() function . In the output result, the index values ​​of the elements are arranged continuously starting from 0.

Summary:

This article introduces how to delete elements from multi-dimensional arrays in PHP. For one-dimensional, two-dimensional, associative arrays and multi-dimensional arrays, we can use the unset() function to delete specified elements. It should be noted that when deleting elements, it may affect the index value of the array. You can use the array_values() function to re-index the array.

The above is the detailed content of How to delete elements from multidimensional 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