Home > Backend Development > PHP Problem > How to convert key into value in php array

How to convert key into value in php array

青灯夜游
Release: 2023-03-16 22:08:01
Original
3369 people have browsed it

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.

How to convert key into value in php array

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);
Copy after login
ParametersDescription
arrayRequired . 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

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;); 
$arr=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
var_dump($arr);
$result=array_flip($arr);
echo "交换后:";
var_dump($result);
?>
Copy after login

How to convert key into value in php array

##Method 2: Use the foreach statement and an empty array to exchange array keys and values

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);  
$arr = array("a"=>"a1","b"=>&#39;b1&#39;,"c"=>"a2","d"=>"a1");
var_dump($arr);
$res=[];
foreach($arr as $k=>$v){
	$res[$v]=$k;
}

echo "交换后:";
var_dump($arr);
?>
Copy after login

How to convert key into value in php array

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)
Copy after login

The parameter description is as follows:

      $array: required parameter, which is the array to be operated;
  • $search_value: Optional parameter. If the parameter 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 $search_value;
  • $strict: Optional parameter to determine whether to use strict mode when searching. $strict defaults to false, which is non-strict mode. Only types are compared during search, not types. If $strict is set to true, that is Strict mode, compares both value and type when searching, equivalent to
  • ===.
array_key() function will return the obtained array key name in the form of an array.

Example 1: All key names


<?php
$arr=array("Peter"=>65,"Harry"=>80,"John"=>78,"Clark"=>90);
var_dump($arr);
var_dump(array_keys($arr));
?>
Copy after login

How to convert key into value in php array

Example 2: Key names of specified values

<?php
$arr=array("Peter"=>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));
?>
Copy after login

How to convert key into value in php array

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)
Copy after login

ParametersDescriptionRequired . Specifies the key value to search for in the array. Required. Specifies the array to be searched. Optional. If this parameter is set to TRUE, the function searches the array for elements of the same data type and value. Possible values:
value
array
strict
    true
  • false - default
  • If set to true, the type of the given value in the array is checked, the number 5 and the string 5 are different (see Example 2).
  • Return value: If the specified key value is found in the array, return the corresponding key name, otherwise return FALSE. If a key value is found more than once in the array, the key name matching the first found key value is returned.


  • <?php
    header(&#39;content-type:text/html;charset=utf-8&#39;);   
    $arr=array("id"=>1,"name"=>"李华","age"=>23);
    var_dump($arr);
    echo "指定值&#39;李华&#39;对应的键名为:".array_search("李华",$arr);
    ?>
    Copy after login

    How to convert key into value in php array

    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!

Related labels:
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