Solved

php merging arrays with the same location or key

Posted on 2011-03-15
3
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

752 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