How To Merge Values of Two Arrays into a Single Array

Q

How To Merge Values of Two Arrays into a Single Array? - PHP Script Tips - PHP Built-in Functions for Arrays

✍: FYIcenter.com

A

You can use the array_merge() function to merge two arrays into a single array. array_merge() appends all pairs of keys and values of the second array to the end of the first array. Here is a PHP script on how to use array_merge():

<?php
$lang = array("Perl", "PHP", "Java",);
$os = array("i"=>"Windows", "ii"=>"Unix", "iii"=>"Mac");
$mixed = array_merge($lang, $os);
print("Merged:\n");
print_r($mixed);
?>

This script will print:

Merged:
Array
(
    [0] => Perl
    [1] => PHP
    [2] => Java
    [i] => Windows
    [ii] => Unix
    [iii] => Mac
)

2007-04-21, 9630👍, 0💬