PHP

Topic: Arrays

How To Join a List of Keys with a List of Values into an Array?

If you have a list keys and a list of values stored separately in two arrays, you can join them into a single array using the array_combine() function. It will make the values of the first array to be the keys of the resulting array, and the values of the second array to be the values of the resulting array. Here is a PHP script on how to use array_combine():<?php$old = array();$old["Zero"] = "PHP";$old[1] = "Perl";$old["Two"] = "Java";$old["3"] = "C+";$old[""] = "Basic";$old[] = "Pascal";$old[] = "FORTRAN";$keys = array_keys($old);$values = array_values($old);print("Combined:\n");$new = array_combine($keys, $values);print_r($new);print("\n");print("Combined backward:\n");$new = array_combine($values, $keys);print_r($new);print("\n");?>This script will print:Combined:Array(    [Zero] => PHP    [1] => Perl    [Two] => Java    [3] => C+    [] => Basic    [4] => Pascal    [5] => FORTRAN)Combined backward:Array(    [PHP] => Zero    [Perl] => 1    [Java] => Two    [C+] => 3    [Basic] =>    [Pascal] => 4    [FORTRAN] => 5

Browse random answers:

How To Create an Array?
How To Test If a Variable Is an Array?
How To Retrieve Values out of an Array?
What Types of Data Can Be Used as Array Keys?
How Values in Arrays Are Indexed?
Can You Add Values to an Array without Keys?
Can You Copy an Array?
How to Loop through an Array?
How the Values Are Ordered in an Array?
How To Copy Array Values to a List of Variables?
How To Get the Total Number of Values in an Array?
How Do You Know If a Key Is Defined in an Array?
How To Find a Specific Value in an Array?
How To Get All the Keys Out of an Array?
How To Get All the Values Out of an Array?
How To Sort an Array by Keys?
How To Sort an Array by Values?
How To Join a List of Keys with a List of Values into an Array?
How To Merge Values of Two Arrays into a Single Array?
How To Use an Array as a Queue?
How To Use an Array as a Stack?
How To Randomly Retrieve a Value from an Array?
How To Create an Array with a Sequence of Integers or Characters?
How To Pad an Array with the Same Value Multiple Times?
How To Truncate an Array?
How To Join Multiple Strings Stored in an Array into a Single String?
How To Split a String into an Array of Substring?
How To Get the Minimum or Maximum Value of an Array?
How can we know the count/number of elements of an array?
How can we know the total number of elements of Array?
Explain Creating and Naming an Array.
What are the different functions in sorting an array?
What are the different functions in sorting an array?
How come the code works, but doesn’t for two-dimensional array of mine? 
What are super global arrays?
Arrays in PHP?