foreach with an AND logical operator

17,271

Solution 1

Just guessing, but try:

if (!empty($txt_name) && !empty($txt_value)) {
    foreach ($txt_name AS $k => $x) {
        $y =$txt_value[$k];

Solution 2

Why doesn't it accept the AND logical operator?

foreach does not execute a logic operation. That's not how it works.

Do I need to create another foreach?

Yes

foreach is a simplified way of writing a more complicated for loop:

foreach ( $arr as $key=>$value )
{
  ...code...
}

is like writing

$keys = array_keys( $arr );
$l = count( $arr );
for ( $i = 0; $i < $l; $i++ )
{
  $key = $keys[$i];
  $value = $arr[$key];
  ...code...
}

Logical operations do not factor into this format.

Solution 3

If they are both arrays, foreach only one of them and use the key to access the other array's value.

foreach ($txt_name AS $key => $name) {
    $value = isset($txt_value[$key]) ? $txt_value[$key] : NULL;
}

Solution 4

array_combine() would probably work best in this particular situation. This would work like the following:

$txt_name=$_REQUEST['text_name'];
$txt_value=$_REQUEST['text_value'];

if (!empty($txt_name) AND ($txt_value)) {
    $values = array_combine($txt_name, $txt_value);
    foreach ($values as $name => $score)  {

        $con = mysql_connect("localhost", "root", "");
        mysql_select_db("test1"); 
        $name = mysql_real_escape_string($name);
        $score = mysql_real_escape_string($score);
        $sql = mysql_query("INSERT INTO tbl_test (testname, score) VALUES ('$name', '$score')");
        mysql_close($con);

    }
}

This combines both variables into one array using the first as the keys and the other array as the values. You can then loop over it using a foreach nice and neatly.

If, later on, however you need to add another value into your SQL query then this technique would not work.

Solution 5

The and logical operator returns true if both its operands are true, and false otherwise.

You seem to not want a logical and, but a concatenation of the two arrays. You could either repeat the loop (less memory use) or use array_merge to produce an array with the values of both arrays.

The names txt_name and txt_value make me think you want to use the two values at the same time though, loop one and retrieve into each of them (though I can't see you using the values anyway).

Share:
17,271
Admin
Author by

Admin

Updated on June 15, 2022

Comments

  • Admin
    Admin almost 2 years

    about foreach, why it doesnt accept the AND logical operator? I've already use the "&&" and I'm getting the same error. do I need to create another foreach?

    <?php
    
    $txt_name=$_REQUEST['text_name'];
    $txt_value=$_REQUEST['text_value'];
    
    if (!empty($txt_name) AND ($txt_value)) {
        foreach ($txt_name AS $x)&&($txt_value AS $y)  {
    
            $con = mysql_connect("localhost", "root", "");
            mysql_select_db("test1"); 
            $name = mysql_real_escape_string($x);
            $score = mysql_real_escape_string($y);
            $sql = mysql_query("INSERT INTO tbl_test (testname, score) VALUES ('$name', '$score')");
            mysql_close($con);
    
        }
    }
    ?>