Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert array value to key by php

Shulou Source: shulou.com Published: 2022-05-31 18:07:37 09月12日 Update

In this article, Xiaobian introduces in detail "how php converts array value to key" with detailed content, clear steps and proper handling of details. I hope that this article "how php converts array value to key" can help you solve your doubts.

Three methods: 1, use the "array_flip ($arr)" statement; 2, use the "array_combine ($arr,array_keys ($arr))" statement; 3, use the "foreach ($arr1 as $k = > $v) {$arr2 [$v] = $k;}" statement.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Php converts the array value to key, that is, converting the keys and values in the array to each other. Here are three methods:

Method 1: use the array_flip () function

The array_flip () function can exchange keys and values in an array

Method 2: use the array_keys () + array_combine () function

Realize the idea:

Use array_keys () to get the key names in the array and return the array of key names

Use array_combine () to merge the original array as the key name and the key name array as the key value to form a new array

Method 3: use foreach loop and an empty array

Read this, the "php how to convert array value to key" article has been introduced, want to master the knowledge of this article also need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.

Tags: Arrays methods functions articles sentences content proper that is location ideas ideas tutorials new knowledge more steps environment computer knowledge knowledge points articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MariaDB MySQL Docker Shulou Information