Get the App
SLTechnology News&Howtos  ›  Development  › 

How to remove the key value of one-dimensional array by php

Shulou Source: shulou.com Published: 2022-06-02 09:37:15 09月13日 Update

This article mainly explains "how to remove the key value of an one-dimensional array by php". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "php how to remove the key value of an one-dimensional array"!

In PHP, each element of an array is made up of key-value pairs (key-value), and the corresponding key value is accessed by the key name of the element.

Tip: "index" and "key name" refer to the same thing, the subscript in the form of multiple exponential groups of numbers.

Sometimes we need to remove the value of an one-dimensional array and leave only the key of the array. What should we do?

At this point, you can use the array_keys () function.

The array_key () function can get some or all of the key names in the array in the following syntax format:

Array_keys ($array [, $search_value = null [, $strict = false]])

The parameters are described as follows:

$array: required parameter, which is the array to be operated

$search_value: optional parameter. If the argument is empty, the function will return all the key names in the array. If this parameter is specified, the function will only return the key name with the value of $search_value

$strict: optional parameter to determine whether strict mode is used when searching. $strict defaults to false, that is, non-strict mode. When searching, only types are compared, not types. If $strict is set to true, strict mode, value and type are compared at the same time when searching, which is equivalent to = =.

The array_key () function returns the resulting array key name as an array.

As you can see, the key value (value) of the one-dimensional array is removed, and only the key name (key) of the one-dimensional array is returned.

Content extension:

1. The unset () function can be used to unset keys and their values in an associative array.

/ declare the associative array $arr = array ("1" = > "plus", "2" = > "minus", "3" = > "multiply", "4" = > "divide"); / / delete key 1 and its value unset ($arr ['1']) in the associative array; / / display the array element var_dump ($arr)

2. Use the array_diff_key () function to get the difference between one or more arrays.

This function compares keys between one or more arrays and returns the difference between them.

/ declare the associative array $arr = array ("1" = > "spring", "2" = > "summer", "3" = > "autumn", "4" = > "winter",); / / remove key 1 and its value $result = array_diff_key ($arr, array_flip ((array) ['1'])) from the associative array; / / display the array element var_dump ($result) At this point, I believe you have a deeper understanding of "how php removes the key value of an one-dimensional array". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Array function one-dimensional parameter association element between content pattern type search that is multiple subtraction form index learning practical deeper subscript Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Microsoft MariaDB Shulou Information