While循环与foreach [英] While loop together with foreach

查看:58
本文介绍了While循环与foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很难解决简单的问题.

I'm really stuck trying to resolve what should be quite simple.

我有这个

<?php 

$json = json_decode('{
                      "33540116":
                                 {"person":
                                           {"name":"John", "age":"36"}},
                      "33541502":
                                 {"person":                                                                                      
                                           {"name":"Jack", "age":"23"}}
                     }
        ');

 $id = array('33540116', '33541502');


foreach($id as $id) {

        echo $json->$id->person->{'name'}. '<br />';
        echo $json->$id->person->{'age'}. '<br />';


        }
?>

因此代码正在解码json字符串,然后使用foreach来回显每个结果.

So the code is decoding a json string then using foreach to echo each result.

这个json文件很大,我只对与mysql表中存储的ID相匹配的某些记录感兴趣.

This json file is rather large and I'm only interested in certain records that match the id's stored in a mysql table.

为此,我将上面的id数组字符串替换为mysql select语句.

To do this I have replaced the id array string above with mysql select statement.

<?php
$json = json_decode('{
                      "33540116":
                                 {"person":
                                           {"name":"John", "age":"36"}},
                      "33541502":
                                 {"person":                                                                                      
                                           {"name":"Jack", "age":"23"}}
                     }
        ');

$result = mysql_query("SELECT id FROM people");
$row = mysql_fetch_array($result);

$id = array($row['id']);

foreach($id as $id) {

            echo $json->$id->person->{'name'}. '<br />';
            echo $json->$id->person->{'age'}. '<br />';


            }
?>

尽管这行得通,但只给了我1个结果.我真正需要的是遍历结果.不幸的是,我不知道如何与foreach一起构建while循环.

Although this works, it only gives me 1 result. What I really need is to loop through the results. Unfortunately I don't know how to construct a while loop together with foreach.

非常感谢您的协助.

更新(其他问题)

谢谢大家.您已经帮助我解决了这个问题.

Thanks everyone. You have helped me solve the problem.

但是,我还有一个与此问题有关的问题.

However, I have another question that relates to this matter.

我在上面提到我只是想回应结果.但这并非完全正确.我真正想做的是使用从json文件检索的结果更新相同的mysql表.

I mentioned above that I merely wanted to echo the results. But this isn't exactly true. What I really want to do is update the same mysql table with the results retreived from the json file.

我有一个叫人的表,其中包含字段ID,名称和年龄.

I have a table called people with fields id, name and age.

如何用这些结果更新该表?

How can I update this table with these results?

再次感谢.

推荐答案

mysql_fetch_array 一次仅获取一行.您可以使用 while 循环继续获取行.一旦获取了整个结果集, mysql_fetch_array 函数将返回 false ,因此这将导致 while 循环根据需要终止.

mysql_fetch_array only fetches one row at a time. You can use a while loop to continue fetching rows. The mysql_fetch_array function returns false once the whole result set has been fetched, so that will cause the while loop to terminate as desired.

我还删除了 $ ids 上的 foreach 循环.由于数组中只有一个元素,因此无需将代码放入循环中.

Also, I removed the foreach loop on $ids. Since there will only be one element in the array it's unnecessary to put the code in a loop.

<?php
$json = json_decode('{
                      "33540116":
                                 {"person":
                                           {"name":"John", "age":"36"}},
                      "33541502":
                                 {"person":                                                                                      
                                           {"name":"Jack", "age":"23"}}
                     }
        ');

$result = mysql_query("SELECT id FROM people");

while ( ( $row = mysql_fetch_array($result) ) ) {
    $id = $row['id'];

    echo $json->$id->person->{'name'}. '<br />';
    echo $json->$id->person->{'age'}. '<br />';
}

?>

这篇关于While循环与foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆