-1

I have 2 arrays:

$gather = $_POST['gather'];
$client_id = $_POST['client_id'];

RESULTS:

Array1 ( [0] => hashtag [1] => followers [2] => latitude [3] => followers [4] => hashtag ) 
Array2 ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) 

I want to use a foreach statement like this:

foreach($gather as $value)
{
    $update = mysql_query("UPDATE gather set gather_choice = '".$value."'  where client_id = '".$client_id[]."'  ")or die(mysql_error());
}

But each of my foreach statement need to take array1[0] and array2[0] .. then array1[1] and array2[1]

In the example, I don't know how to use the '".$client_id[]."' to get the proper value for my sql query...

thanks

user3011784
  • 833
  • 1
  • 8
  • 30

3 Answers3

1

You need to assign the key to a variable in the foreach statement.

foreach($gather as $key => $value) {

}

Then you would use $client_id[$key]. You could also use $gather[$key] which would be equivalent to $value.

I won't copy your SQL statement because it is dangerous and subject to SQL injections and I highly recommend you look into PDO or MySQLi and use prepared statements. You should never use user input ($_POST) directly in a query.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
  • Would this work, even if the arrays have different literal keys? – pradeepcep Nov 23 '14 at 07:27
  • @CEP, I'm not familiar with the term literal keys. Do you mean '1' vs 1? Can you give an example of what you mean? – Devon Bessemer Nov 23 '14 at 07:31
  • 1
    Oh I think I understand what you're saying. Would my example work if they were using associative arrays, not numerical arrays? In that case, only if the keys were the same name. You would have to use the `array_shift` or `array_pop` example if you want to get keys that have a different name but were inserted in the same order. However, in this example, there seems to be no need for an associative array, numerical keys would require less work and checkboxes and form elements with `[]` generate numerical keys. – Devon Bessemer Nov 23 '14 at 07:37
1

Assuming both the arrays are of the same size, you could use something like this:

$gather = $_POST['gather'];
$client_id = $_POST['client_id'];

$total = count($gather);  // count how many elements you have

for ($i = 0; $i < $total; $i++)  // and use a for loop
{
    $update = mysql_query("UPDATE gather set gather_choice = '". $gather[$i] ."'  where client_id = '". $client_id[$i] ."'  ") or die(mysql_error());
}

Or, you could use something like this, and retain the foreach() loop:

$client_id = array_reverse($client_id);  // we reverse the array so that, we can use array_pop on the array, and still get elements in the original order.
foreach($gather as $value)
{
    $update = mysql_query("UPDATE gather set gather_choice = '". $value ."'  where client_id = '". array_pop($client_id) ."'  ") or die(mysql_error());
}

Documentation:


EDIT:

As @Devon said, your query is vulnerable. mysql_* is deprecated. Use mysqli_* or PDO instead.

Please also take a look at the answer to this question: Two arrays in foreach loop

Community
  • 1
  • 1
pradeepcep
  • 902
  • 2
  • 8
  • 24
  • Seems like you're doing extra work to use `array_pop()`. Why wouldn't you just use `array_shift()` in that case? Although a simple `for $i` or `foreach as $key => $value` seems to be a better method to me. – Devon Bessemer Nov 23 '14 at 05:29
  • Nevermind, I see the reason is probably because of the overhead of reindexing using `array_shift()`. Probably not noticeable for an array of <5 elements but still, the for/foreach is probably the fastest. – Devon Bessemer Nov 23 '14 at 05:32
  • I didn't know about `array_shift()` when I wrote the answer (still a noob), but I read up, and it seems `array_reverse()` + `array_pop()` _is indeed faster_ for large sets. http://php.net/manual/en/function.array-shift.php#86783 – pradeepcep Nov 23 '14 at 07:22
  • Also, take a look at this - http://www.evardsson.com/blog/2010/02/05/comparing-php-array_shift-to-array_pop/ – pradeepcep Nov 23 '14 at 07:26
  • yes, looks like I was about right. `array_shift` is faster for smaller arrays and your example is faster for bigger arrays. I think for/foreach would be even faster but if you only have a few elements in the array you are probably talking microseconds and at that point you should focus on readability more than efficiency. – Devon Bessemer Nov 23 '14 at 07:34
0

Just use block below

foreach($gather as $key=>$value)
{
    $update = mysql_query("UPDATE gather set gather_choice = '$value'  where client_id = '$client_id[$key]'  ")or die(mysql_error());
}

Also you to consider in changing to mysqli, is very easy and safe rather than mysql_

Fas M
  • 429
  • 2
  • 11