Solved

PHP array

Posted on 2011-02-14
4
241 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 109

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 109

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

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

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
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.

825 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