Solved

How to Merge 2 Arrays?

Posted on 2010-08-24
5
321 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
ID: 33517544
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
ID: 33517555
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
ID: 33518082
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
ID: 33518121
JFI: ( . ) is used for string concatenation not for array..

0
 
LVL 5

Author Comment

by:eNarc
ID: 33541312
thankyou
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

773 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