Jump to content

Recommended Posts

If I had a simple form like - I would have 2 arrays, title[], caption[]

 

<p>
<input type="text" name="title[]" />
<input type="text" name="caption[]" />
</p>
<p>
<input type="text" name="title[]" />
<input type="text" name="caption[]" />
</p>
<p>
<input type="text" name="title[]" />
<input type="text" name="caption[]" />
</p>
<p>
<input type="text" name="title[]" />
<input type="text" name="caption[]" />
</p>

 

I can access one of these arrays using

 

foreach($_POST['title'] as $key => $value){
//code here
}

 

Is it possible to access both arrays in the same foreach loop. Both the arrays information will be placed in the same DB table so I need a way of putting it on the same row

 

Something like

 

foreach($_POST['title] as $key = > $value && $_POST['caption'] as $key => $value2){
$query = "INSERT INTO table (title, caption) VALUES ('{$input}', '{$value2}')"

$result = mysql_query($query);
}

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/143174-using-foreach-loop-for-two-arrays/
Share on other sites

Look at the array functions.  There are a variety of ways you can combine two arrays together, and of course you could probably write the code to do this manually i'm betting.  Once you have one array, your single foreach will work.

<?php
$arr1 = array(1,2,3,4);
$arr2 = array("one","two","three","four");
$arr3 = array();
for($i=0;$i<=count($arr1)-1;$i++){
	array_push($arr3, $arr1[$i]);
	array_push($arr3, $arr2[$i]);
}
?>

 

I can merge the two arrays together in the correct order but then the array will be twice the size it should be.

 

Code that I want to run 4X will be run 8X  - I'm I right ?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.