$v)" {$arr2[$v]=$k;}" statement."/> $v)" {$arr2[$v]=$k;}" statement.">
search
HomeBackend DevelopmentPHP ProblemHow to convert array value to key in php

How to convert array value to key in php

May 09, 2022 pm 07:31 PM
phpphp array

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.

How to convert array value to key in php

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);
?>

How to convert array value to key in php

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));
?>

How to convert array value to key in php

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);
?>

How to convert array value to key in php

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

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

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor