0

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
---------------------------------------------

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

theres a parse error while running - ibb.co/6B6hh3T ibb.co/DkfDpVs
@Kinja Yeah, forgot a semicolon. Sloppy. 😉

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.