-2

Foreach works with a single arrays, but does not work with the two arrays.. but it required two arrays, this possible ?

Sample : foreach($okeymi as $tokeymi and $sipno as $tsipno)

<input type='checkbox' name='okeymi[]' value='okey'/>
    <input type='checkbox' name='okeymi[]' value='okey'/>
    <input type='checkbox' name='okeymi[]' value='okey'/>
    <input type='checkbox' name='okeymi[]' value='okey'/>
    <input type='text' name='sipno[]'  value='1080'/>
    <input type='text' name='sipno[]'  value='8408'/>
    <input type='text' name='sipno[]'  value='1515'/>
    <input type='text' name='sipno[]'  value='9098'/>


$okeymi = $_POST['okeymi'];
$sipno = $_POST['sipno'];


foreach($okeymi as $tokeymi and $sipno as $tsipno) {

$objConnect = mssql_connect("xxx","xxx","xxx") or die("Error Connect to Database");
$objDB = mssql_select_db("xxxx");


$ftrSQL = "UPDATE [xxxx].[dbo].xxxx SET BAKIYEDEMI='okey' where STOK_KODU='$tokeymi' AND SIPARIS_NO='$tsipno'";

$hbjQuery = mssql_query($ftrSQL);

}
Schizophrenia
  • 47
  • 1
  • 6

4 Answers4

1

In your particular case this will work:

foreach ($array1 as $key=>$value) {
 echo $array1;
 echo $array2[$key];
}
0

No, this isn't possible. How would it work if the arrays contained different numbers of elements?

You can fake it by using a counter, though. Just be careful you don't try to access array elements that don't exist:

$counter = 0;
$max_elements = max(count($okeymi), count($sipno));

while($counter < $max_elements) {
    $tokeymi = isset($okeymi[$i]) ? $okeymi[$i] : null;
    $tsipno = isset($sipno[$i]) ? $sipno[$i] : null;

    #use $tokeymi and $tsipno here

    ++$counter;
}
user428517
  • 4,132
  • 1
  • 22
  • 39
0

Thats wrong. You could do it like that:

$okeymi = $_POST['okeymi'];
$sipno = $_POST['sipno'];
for( $i = 0; $i < count( okeymi ); $i++ )
{ 
   // access your posts $okeymi[ $i ] and $sipno
}
Shlomo
  • 3,880
  • 8
  • 50
  • 82
0

You can't do this. Foreach works on a single array only. IF the two arrays have a 1:1 correspondence between their keys, then you could do something like

foreach($array as $key => $value) {
   $othervalue = $otherarray[$key];
}

Basically loop on one array, then use that array's key to get the matchign value from the other array.

However, it appears your two arrays have different lengths, so this won't work. You can't use an automatic loop to work on both at the same time, because you're working with apples and oranges.

Marc B
  • 356,200
  • 43
  • 426
  • 500