php foreach 循环不会遍历整个数组

Posted

技术标签:

【中文标题】php foreach 循环不会遍历整个数组【英文标题】:php foreach loop does not run through entire array 【发布时间】:2016-12-22 16:54:53 【问题描述】:

测试驱动以下脚本。 php 5.3.3。 mysql 5.0.95。

    <?php 
// show errors in browser
error_reporting(E_ALL);
ini_set('display_errors', 'on');
// connect to database
$conn = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx');
if (!$conn) 
    die("connection failed: " . mysqli_connect_error());

$company_request = 'xxx';
$employee_request = 'xxx';
$timecard_data_results = array();
$fill_old_data_array_def = " SELECT company_id, employee_id, location, task_name, task_start_time, task_end_time, tccomment FROM timecard WHERE company_id = '" . $company_request . "' AND employee_id = '" . $employee_request . "' ORDER BY task_start_time";
$timecard_data_results = mysqli_query($conn, $fill_old_data_array_def);
$iteration = '1';
foreach ($timecard_data_results as $timecard_record) 
    $timecard_record = mysqli_fetch_assoc($timecard_data_results);
    echo $timecard_record['company_id'] . $timecard_record['task_name'] . $timecard_record['task_start_time'];

    echo "  " . $iteration . "<br>";
    $iteration = $iteration + '1';

echo "<br>" . $iteration . "<br>";
echo '<pre>';
print_r($timecard_data_results);
echo '</pre>';
?>

浏览器输出是:

HOSCS:Train Crew:Engineer trainin 2016-10-14 07:00:00 1
HOSCS:Train Crew:Engineer in trai 2016-10-16 10:00:00 2
HOSCS:Train Crew:Engineer in trai 2016-10-17 07:30:00 3
HOSCS:Train Crew:Engineer in trai 2016-10-18 08:15:00 4
HOSCS:Train Crew:Engineer in trai 2016-10-19 09:45:00 5

6
mysqli_result Object
(
    [current_field] => 0
    [field_count] => 7
    [lengths] => Array
        (
            [0] => 3
            [1] => 6
            [2] => 21
            [3] => 31
            [4] => 19
            [5] => 19
            [6] => 1
        )

    [num_rows] => 49
    [type] => 0
)

五次循环后,Foreach 循环似乎退出了。浏览器中没有错误消息。似乎没有脚本被中止。 Print_r 显示数组中的 49 行。 我已经在查询中尝试了不同的排序顺序,以防某些数据对抗循环。结果相同:五条记录。 查询耗时 0.062 秒。 输出中的冒号是来自原始文本文件体系结构的字段的一部分,一旦它将遍历所有行,就需要在 foreach 循环中对其进行解析。 被难住了。

【问题讨论】:

你能用while循环代替foreach吗? mysqli_query 返回一个 mysqli 结果对象。您正在迭代该对象/类,而不是结果本身! 【参考方案1】:

mysqli_result 类直到 PHP 5.4 才实现 Traversable。在 PHP 5.3 中,您将需要使用 while 循环。

<?php 
// show errors in browser
error_reporting(E_ALL);
ini_set('display_errors', 'on');
// connect to database
$conn = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx');
if (!$conn) 
    die("connection failed: " . mysqli_connect_error());

$company_request = 'xxx';
$employee_request = 'xxx';
$timecard_data_results = array();
$fill_old_data_array_def = " SELECT company_id, employee_id, location, task_name, task_start_time, task_end_time, tccomment FROM timecard WHERE company_id = '" . $company_request . "' AND employee_id = '" . $employee_request . "' ORDER BY task_start_time";
$timecard_data_results = mysqli_query($conn, $fill_old_data_array_def);
$iteration = '1';

while($timecard_record = mysqli_fetch_assoc($timecard_data_results))
    echo $timecard_record['company_id'] . $timecard_record['task_name'] . $timecard_record['task_start_time'];

    echo "  " . $iteration . "<br>";
    $iteration = $iteration + '1';



echo "<br>" . $iteration . "<br>";
echo '<pre>';
print_r($timecard_data_results);
echo '</pre>';
?>

【讨论】:

完美。 (早在赶时髦的人发明之前我就听说过这个词)【参考方案2】:

你应该使用

while($timecard_record = mysqli_fetch_assoc($timecard_data_results))

echo $timecard_record['company_id'] . $timecard_record['task_name'] .$timecard_record['task_start_time'];
echo "  " . $iteration . "<br>";
$iteration++;

【讨论】:

同上评论。

以上是关于php foreach 循环不会遍历整个数组的主要内容,如果未能解决你的问题,请参考以下文章

请问php中如何用for循环遍历出关联数组,不要用foreach,用for循环哦

PHP遍历数组常用方式(for,foreach,while,指针等等)

js 循环遍历变量的几种方式

php foreach为啥比for效率高

PHP:替换数组值不会在 foreach 循环后保留

JS中的forEach,for in,for of和for的遍历优缺点及区别