Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert key name and key value into each other by php

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how php converts key names and key values into each other". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how php converts key names and key values into each other.

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

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

Php converts key name and key value to each other

1. Use the array_flip () function

Php converts key names and key values to each other, simply by exchanging key names and key values in an array.

For this operation, there is a built-in function in PHP that is the array_flip () function.

The array_flip () function is used to exchange keys and values in an array, and its syntax is as follows:

Array_flip ($array)

The value in the $array array needs to be able to be a valid key name, such as integer or string. A warning will be issued if the value is of the wrong type, and the problematic key-value pair will not be reversed.

If the same value occurs multiple times, the last key name will be used as its value, and everything else will be lost.

Return value: returns the swapped array on success and NULL on failure.

Example:

2. Use the array_combine () and array_keys () functions

The array_keys () function can get the key names in the array and return the array of key names.

The array_combine () function creates a new array by combining two arrays (an array of key names and an array of key values).

You only need to use the key name array returned by the array_keys () function as the key value of the new array, and the original array as the key name of the new array.

3. Using foreach loop and an empty array

At this point, I believe you have a deeper understanding of "how php converts key names and key values into each other". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report