Solved

PHP array

Posted on 2011-02-14
4
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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.

628 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