Arrays - need to associate value in one with value in another for db insertion

Posted on 2007-11-29
Last Modified: 2013-12-12
Hi - I've got a form that is generated from db data and returns two separate arrays -

(arrID) Array ( [0] => 208 [1] => 210 [2] => 222 [3] => 225 [4] => 236 )
(arrStatus) Array ( [0] => 2 [1] => 3 [2] => 3 [3] => 4 [4] => 1 )

I need to combine or otherwise associate the data in the arrays so that I can properly update the db. For example, I need to do a query similar to:

update table set status = $arrStatus[0] where ID = $arrID[0]

but I want do be able to do this in a foreach statement, as the number of items returned can  differ each time the form is submitted.

Question by:betagirl
  • 3
  • 2
LVL 20

Accepted Solution

steelseth12 earned 500 total points
ID: 20372254

if(count($arrID) == count($arrStatus)) {
		mysql_query("update table set status = ".$arrStatus[$i]." where ID = ".$arrID[$i]);
	print "There was an error.<br> Items in Array arrID dont match items in Array arrStatus";

Open in new window

LVL 20

Expert Comment

ID: 20372257
Opps extra ; on for($i=0;$i<count($arrID);$i++;){

please replace it with


Expert Comment

ID: 20372265
If i understood right:

foreach($arrID as $key => $val){
	$sql = "update table set status = $arrStatus[$key] where ID = $value";
	//execute the sql

Open in new window

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Expert Comment

ID: 20372296
the above sql shoul look like this:
$sql = "update table set status = {$arrStatus[$key]} where ID = $value";

Open in new window


Author Comment

ID: 20375406
@steelseth12 - perfect, just what I needed.

@exmanpl - i needed to associate the values of one array with the values of the  same key  of the other - your query handles only the one array.


Expert Comment

ID: 20380502
No it handles both, you loop through one array and yose the current key value in the other.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Which userID to use for shopping cart application 13 27
IP / 61 56
Inserting data into database 10 36
PHP alternative to file_get_contents('php://input') 4 72
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

828 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