Solved

PHP array

Posted on 2011-02-14
4
243 Views
Last Modified: 2012-05-11
Hello,

I am trying to combine 2 array so I can Implode them together.
$new_array1 contains field names
$new_array2 contains values

example being
$new_array1 = array('id', 'username', 'password');
$new_array2 = array('1', 'tom', 'cat');

$combined_array = array_merge ($new_array1, $new_array2);

print_r($combined_array);

the output I get is:
Array ( [0] => id = [1] => username = [2] => alpha [3] => beta )

it links the array together, but it appends array 2 to array 1.
what I am trying to do is to link both arrays together so

the master array = the first element of array 1 and array 2.

please can someone explain this to me please.

regards,Gavin




$combined_array = array_merge ($new_array1, $new_array2);
print_r($combined_array);
0
Comment
Question by:GPB1983
  • 2
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34887379
Use an iterator like foreach to access the two arrays.  But you have to be absolutely certain that the arrays match the way you want them to!
// UNTESTED CODE
$new = array();
foreach ($array1 as $num => $key)
{
   $new[$key] = $array2[$num];
}

Open in new window

0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 34887396
From this and another question you posted here at EE, I think you might want to stop programming for a little while and get some foundation in how PHP works.  This book is very readable and has great examples.  Give yourself a month to work though it and you will be way ahead in the game.  It will not make you a pro but it will get you started in the right direction.  Now in its fourth printing, it has been a part of my professional library since Version One.
http://www.sitepoint.com/books/phpmysql4/
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 34887398
0
 
LVL 17

Accepted Solution

by:
Chris Harte earned 300 total points
ID: 34887420
0

Featured Post

Industry Leaders: 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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 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