Tools, FAQ, Tutorials:
Joining Keys and Values into an Array in PHP
How To Join a List of Keys with a List of Values into an Array in PHP?
✍: FYIcenter.com
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
)
⇒ Merging Two Arrays into a Single Array in PHP
⇐ Sorting an Array by Values in PHP
2017-01-11, ∼3040🔥, 0💬
Popular Posts:
How to use the XML to JSON Conversion Tool at freeformatter.com? If you want to try the XML to JSON ...
How to extend json.JSONEncoder class? I want to encode other Python data types to JSON. If you encod...
How to start Docker Daemon, "dockerd", on CentOS systems? If you have installed Docker on your CentO...
What are "*..." and "**..." Wildcard Parameters in Function Definitions? If you want to define a fun...
How to register and get an application ID from Azure AD? The first step to use Azure AD is to regist...