Solved

php merging arrays with the same location or key

Posted on 2011-03-15
3
302 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php string masking 4 29
PHP preg_replace code convert to Delphi 14 35
is this a cms? 8 35
remove background quote mark from widget 6 23
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now