Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can merge 3 arrays into a single associative array in PHP

I have 3 arrays, just 7 elements in them each. Arrays are:

filename[]

title[]

description[]

I want to express and iterate through a single associative array for each of the data in the arrays above. filename can be the key value for the assoc array, but each filename has it's own corresponding title and description.

Below is a sample of:

var_dump($filename)
    string(10) "IMG_1676_3" [1]=> 
    string(10) "IMG_0539_3" [2]=> 
    string(8) "IMG_1942" [3]=> 
    string(8) "IMG_1782" [4]=> 
    string(8) "IMG_2114" [5]=> 
    string(8) "IMG_9759" [6]=> 
    string(8) "IMG_2210" }

var_dump($title)
    string(31) "Lighthouse at Ericeira Portugal" [1]=> 
    string(23) "Gaudi park in Barcelona" [2]=> 
    string(32) "Driving around outside of Lisbon" [3]=> 
    string(16) "Madeira Portugal" [4]=> 
    string(15) "Barcelona Spain" [5]=> 
    string(15) "Lisbon Portugal" [6]=> 
    string(14) "Sailing Lisbon" }
like image 897
GivenPie Avatar asked Nov 13 '12 03:11

GivenPie


People also ask

How to merge two arrays into one array in PHP?

The array_merge () function merges one or more arrays into one array. Note: If two or more array elements have the same key, the last one overrides the others. How to merge two arrays without duplicate values in PHP?

What is array_merge() in PHP?

The PHP Array_Merge () function is a built-in php function that is used to merge the elements of one, two, or multiple arrays together into a single array. The elements of the arrays are appended to the end of the previous array in the input list.

How to merge two indexed arrays into one array in MySQL?

1 First, define two indexed arrays: $server_side and $client_side. 2 Second, merge the $server_side and $client_side arrays into one array using the array_merge () function. 3 Third, show the result array.

What happens if two arrays have the same key?

Note: If two or more array elements have the same key, the last one overrides the others. How to merge two arrays without duplicate values in PHP?


2 Answers

function mergeArrays($filenames, $titles, $descriptions) {
    $result = array();

    foreach ( $filenames as $key=>$name ) {
        $result[] = array( 'filename' => $name, 'title' => $titles[$key], 'descriptions' => $descriptions[ $key ] );
    }

    return $result;
}

Just make sure you pass valid input to the function, or add some extra checking. Is that what you're looking for?

like image 85
Colin M Avatar answered Oct 05 '22 23:10

Colin M


If you array key are the same for all the 3 arrays, the better way to do what you are asking, is a foreach creating a new array with all the key(filename,title,description) in the same key:

<?php
foreach($filename as $key => $file)
{
    $files[$key]['filename'] = $file;
    $files[$key]['title'] = $title[$key];
    $files[$key]['description'] = $description[$key];
}
?>
like image 37
DigitalDaigor Avatar answered Oct 05 '22 23:10

DigitalDaigor