• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php array_flip() – Flip the Keys and Values of an Array in php

php array_flip() – Flip the Keys and Values of an Array in php

April 3, 2022 Leave a Comment

The php array_flip() function interchanges the keys and values of an array, making a new array with keys which are the given array’s values and values which are the given array’s keys.

$array = array('bear', 'elephant', 'rabbit');
$flipped_array = array_flip($array1);

print_r($array);
print_r($flipped_array);

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => rabbit
)
Array
(
    [bear] => 0
    [elephant] => 1
    [rabbit] => 2
)

When working with collections of data in php, the ability to manipulate them and create new collections is very valuable.

One such manipulation is the ability to flip the keys and values of an array and create a new array in php.

To flip the keys and values of an array, we can use the php array_flip() function.

The php array_flip() function interchanges the keys and values of an array.

Below is a simple example of using array_flip to flip an array.

$array = array('bear', 'elephant', 'rabbit');
$flipped_array = array_flip($array1);

print_r($array);
print_r($flipped_array);

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => rabbit
)
Array
(
    [bear] => 0
    [elephant] => 1
    [rabbit] => 2
)

Using array_flip() to Remove Duplicates from Array in php

One example of using array_flip() is that you can remove all of the duplicates from a php array with the array_flip() function.

We can take advantage of the fact that an array cannot have duplicate keys and call array_flip() twice.

Below is how to use the array_flip() function to remove duplicates from an array. After using array_flip() twice, we can use array_values() to reindex the array.

$example = array(1,2,4,7,5,4,1,2,3,3,3,4,4,5,5,5,6);

print_r(array_values(array_flip(array_flip($example))));

//Output:
Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 7
    [4] => 5
    [5] => 3
    [6] => 6
)

Hopefully this article has been useful for you to learn about how to use the php array_flip() function.

Other Articles You'll Also Like:

  • 1.  php rtrim() Function – Remove Whitespace at End of String
  • 2.  php array_intersect() – Find Intersection of Two or More Arrays in php
  • 3.  Remove Duplicates from Array in php with array_unique()
  • 4.  php preg_match – Find Pattern in String Using Regex (Regular Expression)
  • 5.  php session_destroy() – Delete Session File Where Session Data is Stored
  • 6.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]
  • 7.  php sin – Find Sine of Number in Radians Using php sin() Function
  • 8.  php Delete Files with php Unlink Function
  • 9.  php Convert Radians to Degrees with php rad2deg() Function
  • 10.  Replace Space with Dash in php String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy