Homer_J
Homer_J

Reputation: 3323

PHP Loop with While Loop confusion

I currently have a simple WHILE loop running as follows:

while($los = $result->fetch_row())
    {
        echo"<tr><td>".$los[0]."</td>";
        echo"<td>".$los[1]."</td>";
        echo"<td>".$los[2]."</td></tr>";
    }

The contents of $los is as follows:

London    => 15 => 32
Glasgow   => 23 => 45
Leeds     => 1  => 12
Truro     => 5  => 23

All working fine but what I am struggling with is that I have a second array $outs, the contents of which are:

London    => 3 
Glasgow   => 5 
Liverpool => 2
Poole     => 1

Within the first while loop I am trying to subtract the value for $outs from $los when the location name matches. I have tried adding the following inside the while loop but no joy:

if($los[0] == $outs[0]){
    $los[1] = $los[1]-$outs[1];
}

But no joy, also when I have tried print_r($outs) from within the while loop, all it returns is:

Poole     => 1
Poole     => 1
Poole     => 1
Poole     => 1

I cannot fathom where I am going wrong or even if this is possible. Is the $outs array being modified as it's within the first loop? Any ideas, suggestion or pointers welcomed on how I might achieve this.

VAR DUMPS

So, as requested the contents of the two arrays are:

$los

London    => 15 => 32
Glasgow   => 23 => 45
Leeds     => 1  => 12
Truro     => 5  => 23

$outs

London    => 3 
Glasgow   => 5 
Liverpool => 2
Poole     => 1

However, when I vardump or print_r for $outs when it is in the while($los = $result->fetch_row()) the contents are:

Poole     => 1
Poole     => 1
Poole     => 1
Poole     => 1

FURTHER CODE

The code to obtain the array for $outs is:

$query19 = "SELECT Country, COUNT(Country), Resp FROM `tresults_` WHERE q39 = 'Complete' GROUP BY Country;";

$result19 = $mysqli->query($query19);

while($row19 = $result19->fetch_assoc()){
    $outs = $row19;
}

Upvotes: 2

Views: 72

Answers (1)

cb0
cb0

Reputation: 8613

Please consider to post the RAW Content that was produced by var_dump and include a full but minimal script to produce the error.

What is unclear here seem to be if $outs is a (1) Key-Value Array or a if it's (2) containing sub-arrays.

Option 1:

$outs = [
    "London"    => 3,
    "Glasgow"   => 5,
    "Liverpool" => 2,
    "Poole"     => 1
];

Option 2:

$outs = [
    ["London", 3],
    ["Glasgow", 5],
    ["Liverpool", 2],
    ["Poole", 1]
];

I did for testing assume your input rows look like this:

$citys = [
    ["London", 15, 32],
    ["Glasgow", 23, 45],
    ["Leeds", 1, 12],
    ["Truro", 5, 23]
];

Depending on this you sould adjust the if inside your while loop.

For option 1:

foreach ($citys as $los) {
    if (array_key_exists($los[0], $outs)) {
        $los[1] = $los[1] - $outs[$los[0]];
    }

    echo " <tr><td > " . $los[0] . "</td > ";
    echo "<td > " . $los[1] . "</td > ";
    echo "<td > " . $los[2] . "</td ></tr > ";
}

For option 2 it's a little bit more code, as we need to search for the cityname index inside $outs first. In php > 5.5 it could be done using array_column which would be easier.

foreach ($citys as $los) {
    $indexInOuts = null;
    foreach($outs as $index => $out){
        if($los[0] === $out[0]){
            $indexInOuts = $index;
            break;
        }
    }

    if($indexInOuts !== null){
        $los[1] = $los[1] - $outs[$indexInOuts][1];
    }

    echo " <tr><td > " . $los[0] . "</td > ";
    echo "<td > " . $los[1] . "</td > ";
    echo "<td > " . $los[2] . "</td ></tr > ";
}

The output for both options would be like:

London 12 32 
Glasgow 18 45 
Leeds 1 12 
Truro 5 23

And as stated in the comments you are replacing the $outs every time. So edit it to look like this:

$outs = [];
while ($row19 = $result19->fetch_assoc()) {
   $outs[] = $row19;
}

Upvotes: 1

Related Questions