Solved

php merging arrays with the same location or key

Posted on 2011-03-15
3
326 Views
Last Modified: 2012-05-11
I have to following two arrays that I would like to combine using php. How do I do this?

Array1
(
    [0] => Location 1
    [1] => Streamflow
)
, Array2
(
    [0] => Location 1
    [1] => Gage
)


Combine the arrays into a single array like this:
Array
(
    [0] => Location 1
    [1] => Streamflow
    [2] => Gage
)


Thanks!
0
Comment
Question by:kent3800
3 Comments
 
LVL 9

Expert Comment

by:Vampireofdarkness
ID: 35144910
As long as you're omitting 'Location 1' as the text is the same:

	$a1=array(0=>'Location 1',1=>'Streamflow');
	$a2=array(0=>'Location 1',1=>'Gage');

	foreach($a2 as $key => $value) {
		if(!in_array($value, $a1)) {
			$a1[]=$value;
		}
	}

	print_r($a1);

Open in new window

0
 
LVL 3

Accepted Solution

by:
Manikandan1986 earned 500 total points
ID: 35145189
Try this

<?php

$a1=array(0=>'Location 1',1=>'Streamflow');
$a2=array(0=>'Location 1',1=>'Gage');

$a3 = array_merge($a1, $a2);
echo "Merged Array : <br>";
print_r($a3);
echo "<br>Merged Unique Array : <br>";
$a4 = array_unique($a3);
print_r($a4);

?>

Output:

Merged Array :

Array
(
    [0] => Location 1
    [1] => Streamflow
    [2] => Location 1
    [3] => Gage
)

Merged Unique Array :


Array
(
    [0] => Location 1
    [1] => Streamflow
    [2] => Gage
)
0
 
LVL 4

Author Closing Comment

by:kent3800
ID: 35409829
Awesome. Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question