CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   PHP   How to Merge Two Arrays into Single Associative Array in PHP ...

How to Merge Two Arrays into Single Associative Array in PHP ?

July 8, 2018 by SNK

Today in this tutorial we are going to merge two different arrays into single associative array in PHP.

Last time i was creating travel based website and creating own engine in which the condition holiday package was to merge two different array of title an description into single array.

To know the clear example on what i am saying is that “title” was in one array and “description” was in another array similar to given example below.

Array.php
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$titles = array(
'Day 1',
'Day 2',
'Day 3',
'Day 4',
'Day 5',
'Day 6'
);
 
$descriptions = array(
'Day One Itinerary Description',
'Day Two Itinerary Description',
'Day Three Itinerary Description',
'Day Four Itinerary Description',
'Day Five Itinerary Description',
'Day Six Itinerary Description'
 
);

Out target is to merge the both arrays into one as title and description.

MergeArrayExample
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Array
(
    [0] => Array
        (
            [title] => Day 1
            [description] => Day One Itinerary Description
        )
 
    [1] => Array
        (
            [title] => Day 2
            [description] => Day Two Itinerary Description
        )
 
    [2] => Array
        (
            [title] => Day 3
            [description] => Day Three Itinerary Description
        )
 
    [3] => Array
        (
            [title] => Day 4
            [description] => Day Four Itinerary Description
        )
 
    [4] => Array
        (
            [title] => Day 5
            [description] => Day Five Itinerary Description
        )
 
    [5] => Array
        (
            [title] => Day 6
            [description] => Day Six Itinerary Description
        )
 
)

So what we are going to do loop through the first array and then use the key of first array to arrange the correct title and description in order.

Lets create the function to merge and sort them properly.

MergedArrayFunc
PHP
1
2
3
4
5
6
7
8
9
10
function mergeArrays($titles, $descriptions) {
    $result = array();
 
    foreach ( $titles as $key=>$title ) {
        $result[] = array( 'title' => $title, 'description' => $descriptions[$key]);
    }
    return $result;
}
 
$data = mergeArrays($titles,$descriptions);

Here you go, just var_dump() or print_r() to know the result.

SHARE ON
Buffer

Enjoyed this article?

Like us on

PHP merge two arrays in php php array same key different value php merge associative arrays

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

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

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.