Kinja
Kinja

Reputation: 459

&& in Foreach loop (PHP)

Just stating learning PHP, I would like to know is there any way I can declare more than 1 $value in a foreach loop? I am trying to echo out my 8 different type of arrays which i declared ($line0 - $line8). Apologies if my codes are abit messy. I'm still new to PHP.

PHP Code

<?php
    
$handle = @fopen('listings.txt', "r");
$row = 0;
$count = 0;
$line0 = [];
$line1 = [];
$line2 = [];
$line3 = [];
$line4 = [];
$line5 = [];
$line6 = [];
$line7 = [];
$line8 = [];
    
if ($handle) { 
   while (!feof($handle)) { 
       $store = fgets($handle, 4096); 
       if ($row == 9){
        $row = 0;
        $count++;
    }
    if ($row == 0) 
    {
        $line0[] = strval($store);
    }
    else if($row == 1) {
$line1[] = strval($store);}
    else if($row == 2) {
$line2[] = strval($store);}
    else if($row == 3) {
$line3[] = strval($store);}
    else if($row == 4) {
$line4[] = strval($store);}
    
    $row++;
   }
    ?>

    <table>     
        <tr>
        <?php
        foreach($line2 as $value1)&&(line3 as $value2){

                echo "<td><b>Product ID: $value1</b>"
                echo "<td><b>Selection ID: $value2</b>

                </td>";
                echo '</tr>';
            }
        ?>
    </table>

listings.txt

Cedric
93482812
[email protected]
Guitar
---------------------------------------------
Wendy
98238432
[email protected]
Guitar
---------------------------------------------

Upvotes: 0

Views: 103

Answers (1)

KIKO Software
KIKO Software

Reputation: 16686

No you can't do that, but there's something else you can use. All your arrays have the name keys, and you can get the key with foreach like this:

foreach ($line1 as $key => $value1) {
    $value2 = $line2[$key];
    echo "<tr>";
    echo "<td><b>Product ID: $value1</b></td>";
    echo "<td><b>Selection ID: $value2</b></td>";
    echo '</tr>';
}

That is getting pretty close to what you want.

Upvotes: 2

Related Questions