Solved

How to Merge 2 Arrays?

Posted on 2010-08-24
5
317 Views
Last Modified: 2012-05-10
Hi, I've got 2 Arrays, one is Session and the other is post.

for example.

$_SESSION['data'];
$_POST['data'];

first I begin like so.

$_SESSION['data']=$_POST['data'];



now when I do this.

$_SESSION['data'].=$_POST['data'];

just get array array array array etc?

how I merge them?
0
Comment
Question by:eNarc
5 Comments
 
LVL 51

Accepted Solution

by:
HainKurt earned 250 total points
Comment Utility
try this

$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 250 total points
Comment Utility
check here for user comments for diffrent solutions, bug fixes...

http://php.net/manual/en/function.array-combine.php
0
 
LVL 1

Assisted Solution

by:bonjourjolie
bonjourjolie earned 125 total points
Comment Utility
there is few ways to merge arrays.

1. array combine. 1 array will become row & other array will become Column
Ex:
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
print_r($c);
Result;
Array
(    [green]  => avocado
    [red]    => apple
    [yellow] => banana)
*****************************************
2. array_merge. B array will become continues of A array.

$beginning = 'red';
$end = array(1 => 'yellow');
$result = array_merge((array)$beginning, (array)$end);
print_r($result);

Array
    (   [0] => red
        [1] => yellow
    )
0
 
LVL 12

Assisted Solution

by:Avinash Zala
Avinash Zala earned 125 total points
Comment Utility
JFI: ( . ) is used for string concatenation not for array..

0
 
LVL 5

Author Comment

by:eNarc
Comment Utility
thankyou
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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

9 Experts available now in Live!

Get 1:1 Help Now