Home>Article>Backend Development> How to convert key into value in php array
3 conversion methods: 1. Use array_flip() to exchange the positions of keys and values, the syntax is "array_flip (array)"; 2. Use the foreach statement and an empty array to exchange the positions of keys and values, the syntax "foreach($arr as $k=>$v){$r[$v]=$k;}"; 3. Use array_keys() to get all the keys of the array, the syntax is "array_keys(array)" , will return an array containing all key names.
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
Situation 1: Array Converting key to value can be understood as exchanging key and value.
Method 1: Use the array_flip() function to exchange array keys and values
array_flip() function is used to reverse/exchange the keys in the array Key name and corresponding associated key value.
array_flip(array);
Parameters | Description |
---|---|
array | Required . Specifies the array whose key/value pairs need to be reversed. |
Return value: If the reversal is successful, the reversed array is returned; if the reversal fails, NULL is returned.
Note: We must remember that the values of the array must be valid keys, i.e. they must be integers or strings. If a value is of the wrong type, a warning will be thrown and the associated key/value pair will not be included in the result.
Example
"red","b"=>"green","c"=>"blue","d"=>"yellow"); var_dump($arr); $result=array_flip($arr); echo "交换后:"; var_dump($result); ?>
##Method 2: Use the foreach statement and an empty array to exchange array keys and values
"a1","b"=>'b1',"c"=>"a2","d"=>"a1"); var_dump($arr); $res=[]; foreach($arr as $k=>$v){ $res[$v]=$k; } echo "交换后:"; var_dump($arr); ?>
Scenario 1: The array converts the key (key) into a value, or you can simply understand that the key (key) is retained as the element value
can be understood as removing the original value, leaving only the key as the element value. At this point, you need to use the array_keys() function. array_key() function can obtain some or all key names (subscripts) in the array. The syntax format of this function is as follows:array_keys($array,$search_value,$strict)The parameter description is as follows:
.
65,"Harry"=>80,"John"=>78,"Clark"=>90); var_dump($arr); var_dump(array_keys($arr)); ?>Example 2: Key names of specified values
65,"Harry"=>80,"John"=>78,"Clark"=>90); var_dump($arr); var_dump(array_keys($arr,80)); var_dump(array_keys($arr,"80")); var_dump(array_keys($arr,"80",true)); ?>
Method 3: Use array_search() function to query
array_search() function can search for the specified key value in the array and return the corresponding key name.array_search(value,array,strict)
Description | |
---|---|
value | Required . Specifies the key value to search for in the array.|
array | Required. Specifies the array to be searched.|
strict | Optional. If this parameter is set to TRUE, the function searches the array for elements of the same data type and value. Possible values:
|
1,"name"=>"李华","age"=>23); var_dump($arr); echo "指定值'李华'对应的键名为:".array_search("李华",$arr); ?>Recommended study: "
PHP Video Tutorial"
The above is the detailed content of How to convert key into value in php array. For more information, please follow other related articles on the PHP Chinese website!