开发者

php foreach looping twice

I am trying to loop through some data from my database but it is outputting it twice.

$fields = 'field1, field2, field3, field4';
$idFields = 'id_field1, id_field2, id_field3, id_field4';
$tables = 'table1, table2, table3, table4';
$table = explode(', ', $tables);
$field = explode(', ', $fields);
$id = explode(', ', $idFields);
$str = 'Egg';

$i=0;

while ($i<4) {  
    $f = $field[$i];
    $idd = $id[$i];
    $sql = $writeConn->select()->from($table[$i], array($f, $idd))->where($f . " LIKE ?", '%' . $str . '%');
    $string = '<a title="' . $str . '" href="' . $currentProductUrl . '">' . $str . '</a>';
    $result = $writeConn->fetchAssoc($sql); 

        foreach ($result as $row) {
            echo 'Success! Found ' . $str . ' in ' . $f . '. ID: ' . $row[$idd] . '.<br>';
        }
    $i++;
}

Outputting:

Success! Found Egg in field3. ID: 5.

Success! Found Egg in field3. ID: 5.

Could someone please explain why it is looping through both the indexed and associative values?

UPDATE

I did some more playing around and tried the following.

$fields = 'field1, field2, field3, field4';
$idFields = 'id_field1, id_field2, id_field3, id_field4';
$tables = 'table1, table2, table3, table4';
$table = explode(', ', $tables);
$field = explode(', ', $fields);
$id = explode(', ', $idFields);
$str = 'Egg';

$i=0;

while ($i<4) { 
    $f = $field[$i];
    $idd = $id[$i];
    $sql = $writeConn->select()->from($table[$i], array($f, $idd))->where($f . " LIKE ?", '%' . $str . '%');
    $string = '<a title="' . $str . '" href="' . $currentProductUrl . '">' . $str . '</a>';
    $sth = $writeConn->prepare($sql);
    $sth->execute();
    $result = $sth->fetch(PDO::FETCH_ASSOC);

        foreach ($result as $row) {
            echo 'Success! Found ' . $str . ' in ' . $f . '. ID: ' . $row[$idd] . '.<br>';
        }
    $i++;
}

The interesting thing is that this outputs the below:

Success! Found Egg in field3. ID: E.

Success! Found Egg in field3. ID: E.

Success! Found Egg in field3. ID: 5.开发者_高级运维

Success! Found Egg in field3. ID: 5.

Success! Found Egg in field3. ID: E.

Success! Found Egg in field3. ID: E.

Success! Found Egg in field3. ID: 5.

Success! Found Egg in field3. ID: 5.

I have also tried adding $i to the output and this outputs 2 as expected. If I change fetch(PDO::FETCH_BOTH) to fetch(PDO::FETCH_ASSOC) the output is as follows:

Success! Found Egg in field3. ID: E.

Success! Found Egg in field3. ID: E.

Success! Found Egg in field3. ID: 5.

Success! Found Egg in field3. ID: 5.

This has been bugging me for too long, so if anyone could help I would be very appreciative!


It's probably fetchAssoc() returning an array with both numeric (0~n) and string (column name) keys. See PDO::FETCH_BOTH on the PDOStatement::fetch() documentation page.

Addition: And the reason for this is the same one for why fetchAssoc() returns a double array by default: PHP makes no distinction between numerically- and string-indexed arrays; it's up to you to use either.


Forget all the complicated explanations. Your problem is caused by using echo() inside that loop. I had the same problem (see below) where the foreach loop was being run twice for no apparent reason.

IF you want to provide information from inside your PHP code for testing purposes, create a text string and add stuff to it like:

$result .= "<br>And then this thingy did those stuff";

If you do this and only echo the $result at the end of the process, you'll find that your problem has disappeared.

The echo() function messes with PHP's sense of accomplishment in a quantum kind of way: you make it feel like it's simultaneously already done running your code, and like it hasn't started yet. I can't explain the details, but the outcome is pretty quarky.

MY PROBLEM AS IT WAS: I'm having the same problem with a different setup of foreach. In my case the array comes from a HTTP request URL:

//myfakeserver/functions/dealWithIncomes.php?i={%220%22:{%22amount%22:332,%22T1%22:%22month%22,%22M1%22:1,%22T2%22:%22dayofmonth%22,%22M2%22:20,%22T3%22:%22%22,%22M3%22:0,%22S1%22:%22%22,%22E1%22:%22%22},%221%22:{%22amount%22:444,%22T1%22:%22week%22,%22M1%22:1,%22T2%22:%22dayofweek%22,%22M2%22:1,%22T3%22:%22%22,%22M3%22:0,%22S1%22:%22%22,%22E1%22:%22%22},%222%22:{%22amount%22:443,%22T1%22:%22year%22,%22M1%22:1,%22T2%22:%22%22,%22M2%22:0,%22T3%22:%22%22,%22M3%22:0,%22S1%22:%222012-02-29%22,%22E1%22:%22%22}}

$incomes = json_decode($_GET['i'],true);

In other words, a JSON string representing three incomes and their properties like amount and recurrence pattern.

PHP understands this string as an array very well: sizeof($incomes) returns 3.

But when I loop through this thing...

foreach($incomes as $income){ $result .= "Dealing with one income from array with length " . sizeof($incomes); }

...I get... "Dealing with one income from array with length 3" "Dealing with one income from array with length 3" "Dealing with one income from array with length 3" "Dealing with one income from array with length 3" "Dealing with one income from array with length 3" "Dealing with one income from array with length 3"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜