Home  >  Article  >  Backend Development  >  How to reverse array values ​​in php

How to reverse array values ​​in php

藏色散人
藏色散人Original
2020-09-29 10:14:362791browse

In PHP, you can use the array_flip function to reverse the key names and corresponding associated key values ​​in the array. The syntax is "array_flip(array);". The parameter array specifies the array that needs to be reversed.

How to reverse array values ​​in php

Recommendation: "PHP Video Tutorial"

array_flip() function is used to reverse/exchange elements in an array Key name and corresponding associated key value.

Syntax

array_flip(array);

Parameters

array required. Specifies the array whose key/value pairs need to be reversed.

Technical details

Return value: If the inversion is successful, the reversed array is returned. If the reversal fails, NULL is returned.

PHP version: 4

Example:

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$result=array_flip($a1);
print_r($result);
?>

Run result:

Array ( [red] => a [green] => b [blue] => c [yellow] => d )

The above is the detailed content of How to reverse array values ​​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