Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.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 为什么在mysqli结果对象上的第一次迭代后foreach循环会停止?_Php_Arrays_Mysqli_Nested - Fatal编程技术网

Php 为什么在mysqli结果对象上的第一次迭代后foreach循环会停止?

Php 为什么在mysqli结果对象上的第一次迭代后foreach循环会停止?,php,arrays,mysqli,nested,Php,Arrays,Mysqli,Nested,我有一组mysqli结果,我正在迭代这些结果,以创建每个嵌套数组中具有所有相同顺序id的嵌套数组 我用这个结构得到了第一个结果,但正如我之前所说的,迭代在第一个结果之后停止 下面是我用来构造嵌套数组的代码: $ress = $results; $count = 0; foreach($results as $result){ echo $count . "<br>"; $orderId = $result['orderId']; $records[$coun

我有一组mysqli结果,我正在迭代这些结果,以创建每个嵌套数组中具有所有相同顺序id的嵌套数组

我用这个结构得到了第一个结果,但正如我之前所说的,迭代在第一个结果之后停止

下面是我用来构造嵌套数组的代码:

$ress = $results;
$count = 0;
foreach($results as $result){
    echo $count . "<br>";
    $orderId = $result['orderId'];
    $records[$count] = array();
    foreach($ress as $r){
        if($r['orderId'] == $orderId and !in_array($r, $records[$count])){
            array_push($records[$count], $r);
        }
    }
    $count += 1;
    $results->data_seek($count);
}
$ress=$results;
$count=0;
foreach($results作为$result){
echo$count。“
”; $orderId=$result['orderId']; $records[$count]=array(); foreach($r){ if($r['orderId']=$orderId和!在数组中($r$records[$count])){ 数组推送($records[$count],$r); } } $count+=1; }

有人知道为什么在第一次迭代后会停止吗?

因为结果集不是数组(虽然它是可编辑的),而是资源。但是,当您到达结果集的末尾时,需要在再次迭代之前手动将其重置为开头

在第一次检索到
$result
之后,您将迭代整个
$ress
(减去第一条记录),因此您需要重置resultset指针,以便能够访问下一个
$result
,因为$ress和$results都指向同一资源

用于在
$count+=1之后立即将结果集重置回
$count

$ress=$results;
$count=0;
foreach($results作为$result){
echo$count。“
”; $orderId=$result['orderId']; $records[$count]=array(); foreach($r){ if($r['orderId']=$orderId和!在数组中($r$records[$count])){ 数组推送($records[$count],$r); } } $count+=1; $results->data\u seek($count); }
请也添加请求代码(SQL)@Spoke44,我更新了问题。就是这样!我没有意识到我必须重置指针。谢谢这仍然很难看。首先将记录提取到数组中,这样就不会再出现循环问题。
mysqli_result Object
(
    [current_field] => 0
    [field_count] => 83
    [lengths] => 
    [num_rows] => 478
    [type] => 0
)
$ress = $results;
$count = 0;
foreach($results as $result){
    echo $count . "<br>";
    $orderId = $result['orderId'];
    $records[$count] = array();
    foreach($ress as $r){
        if($r['orderId'] == $orderId and !in_array($r, $records[$count])){
            array_push($records[$count], $r);
        }
    }
    $count += 1;
}
$ress = $results;
$count = 0;
foreach($results as $result){
    echo $count . "<br>";
    $orderId = $result['orderId'];
    $records[$count] = array();
    foreach($ress as $r){
        if($r['orderId'] == $orderId and !in_array($r, $records[$count])){
            array_push($records[$count], $r);
        }
    }
    $count += 1;
    $results->data_seek($count);
}