3 methods: 1. Use the "array_flip($arr)" statement; 2. Use the "array_combine($arr,array_keys($arr))" statement; 3. Use "foreach($arr1 as $ k=>$v){$arr2[$v]=$k;}" statement.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php converts the array value into key, that is Convert the keys and values in the array to each other. Three methods are introduced below:
Method 1: Use array_flip() function
array_flip() function can exchange keys and values in the array
<?php $arr1=array("aaa"=>11,"bbb"=>22,"ccc"=>33); var_dump($arr1); $arr2=array_flip($arr1); var_dump($arr2); ?>
Method 2: Use array_keys() array_combine() function
Implementation idea:
Use array_keys () Obtain the key name in the array and return the key name array
Use array_combine() to use the original array as the key name and the key name array as the key value to merge into a new array
<?php $arr=array("Peter"=>11,"Ben"=>22,"Joe"=>33); var_dump($arr); $keys=array_keys($arr); var_dump(array_combine($arr,$keys)); ?>
Method 3: Using a foreach loop and an empty array
<?php $arr1=array("aaa"=>11,"bbb"=>22,"ccc"=>33); $arr2=array(); foreach($arr1 as $k=>$v){ $arr2[$v]=$k; } var_dump($arr2); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert array value to key in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor
