Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:JSON返回空值_Php_Json_For Loop_Mysqli_While Loop - Fatal编程技术网

PHP:JSON返回空值

PHP:JSON返回空值,php,json,for-loop,mysqli,while-loop,Php,Json,For Loop,Mysqli,While Loop,我目前正在为我的网站开发一个搜索功能,并以JSON返回结果。但是,某些值在返回第一个结果后返回,如下所示: {"fullname":"test name","occupation":"test","industry":"testing","bio":"i am testing stuff.","gender":"f","website":"http:\/\/yhisisasite.com","skills":["writing","reading","math","coding","baseba

我目前正在为我的网站开发一个搜索功能,并以JSON返回结果。但是,某些值在返回第一个结果后返回,如下所示:

{"fullname":"test name","occupation":"test","industry":"testing","bio":"i am testing stuff.","gender":"f","website":"http:\/\/yhisisasite.com","skills":["writing","reading","math","coding","baseball"],"interests":["coding","sampling","googling","typing","playing"]},{"fullname":null,"occupation":null,"industry":null,"bio":null,"gender":null,"website":null,"skills":["coding","docotrs","soeku","spelling"],"interests":["testing","wintro","skating","hockey","code"]}
我目前有一个类作为结果的模板,如下所示:

class SearchResultUserProfile {
    public $fullname = "";
    public $occupation = "";
    public $industry = "";
    public $bio = "";
    public $gender = "";
    public $website = "";
    public $skills = array();
    public $interests = array();

}
然后,为了填充这些字段,我在mysqli获取期间有几个循环:

while ($row = mysqli_fetch_array($result))
{   
    $max = sizeof($user_id_array);
    for($i = 0; $i < $max; $i++)
    {
    //create a new instance or object
    $searchResultUserProfile = new SearchResultUserProfile();
    $searchResultUserProfile->fullname = $row['fullname'];
    $searchResultUserProfile->occupation = $row['occupation'];
    $searchResultUserProfile->industry = $row['industry'];
    $searchResultUserProfile->bio = $row['bio'];
    $searchResultUserProfile->gender = $row['gender'];
    $searchResultUserProfile->website = $row['website'];

    //grab the interests and skills
    $skillDetails = fetchAllUserSkills($user_id_array[$i]);
    foreach($skillDetails as $row) {
        $thistest = $row['skills'];
        array_push($searchResultUserProfile->skills, $thistest);
    }

    $interestDetails = fetchAllUserInterests($user_id_array[$i]);
    foreach($interestDetails as $row) {
        $thistests = $row['interests'];
        array_push($searchResultUserProfile->interests, $thistests);
    }
    array_push($results, $searchResultUserProfile);

    }
    echo json_encode($results);
}
while($row=mysqli\u fetch\u数组($result))
{   
$max=sizeof($user\u id\u数组);
对于($i=0;$i<$max;$i++)
{
//创建新实例或对象
$searchResultUserProfile=新的searchResultUserProfile();
$searchResultUserProfile->fullname=$row['fullname'];
$searchResultUserProfile->occupation=$row['occupation'];
$searchResultUserProfile->industry=$row['industry'];
$searchResultUserProfile->bio=$row['bio'];
$searchResultUserProfile->gender=$row['gender'];
$searchResultUserProfile->website=$row['website'];
//抓住兴趣和技能
$skillDetails=fetchAllUserSkills($user_id_array[$i]);
foreach($skillDetails作为$row){
$thisttest=$row['skills'];
数组推送($searchResultUserProfile->skills,$thistest);
}
$interestDetails=fetchAllUserInterest($user\u id\u array[$i]);
foreach(利息明细为$row){
$thistests=$row['interests'];
数组推送($searchResultUserProfile->interests,$thistests);
}
array_push($results,$searchResultUserProfile);
}
echo json_编码($results);
}

知道为什么会这样吗?这是我迭代循环还是设置的方式?我确信我忽略了一些简单的事情,但我不知道它是什么

问题是您正在覆盖内部循环中的
$row
变量:

while ($row = mysqli_fetch_array($result))
       ^^^^ this is a result row from your query
{   
    $max = sizeof($user_id_array);
    for($i = 0; $i < $max; $i++)
    {
        $searchResultUserProfile = new SearchResultUserProfile();
        $searchResultUserProfile->fullname = $row['fullname'];

        ...

        foreach($skillDetails as $row) {
                                 ^^^^ here you are overwriting your query result
           ...
        }

        ...

    }
    echo json_encode($results);
}
while($row=mysqli\u fetch\u数组($result))
^^^^这是查询的结果行
{   
$max=sizeof($user\u id\u数组);
对于($i=0;$i<$max;$i++)
{
$searchResultUserProfile=新的searchResultUserProfile();
$searchResultUserProfile->fullname=$row['fullname'];
...
foreach($skillDetails作为$row){
^^^^这里您正在覆盖查询结果
...
}
...
}
echo json_编码($results);
}

因此,如果
$max
大于1,则从第二次迭代开始,您将使用上一个内部循环的最后一个结果。这将不是您期望的查询结果。

您确定
$results
json\u encode
之前不为空吗?不相关,但要生成有效的json,您需要将最后一条语句放在
while
循环之外(如果有多个结果)。