Solved

PHP array

Posted on 2011-02-14
4
239 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
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
Comment Utility
0
 
LVL 16

Accepted Solution

by:
Chris Harte earned 300 total points
Comment Utility
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

15 Experts available now in Live!

Get 1:1 Help Now