Php 另一个未定义的偏移错误

Php 另一个未定义的偏移错误,php,Php,我以正确的格式返回JSON,但是我得到了一个 注意:未定义的偏移量2 当我浏览网页时。我昨晚花了一整晚的时间试图解决这个问题,我可以让错误消失,但我只得到一行数据,而不是所有的数据。我尝试将密钥名称更改为数字,但仍然无法使其正常工作 #part of a factory pattern thats called by getIt() public function selectAll($where='') { $stmt = $this->dbc->prepare("SELECT *

我以正确的格式返回JSON,但是我得到了一个

注意:未定义的偏移量2

当我浏览网页时。我昨晚花了一整晚的时间试图解决这个问题,我可以让错误消失,但我只得到一行数据,而不是所有的数据。我尝试将密钥名称更改为数字,但仍然无法使其正常工作

#part of a factory pattern thats called by getIt()
public function selectAll($where='')
{
$stmt = $this->dbc->prepare("SELECT * FROM {$where}");
$stmt->execute();
$this->results = $stmt->fetchAll();
return $this;
}

#cheap-api.php
$output = $work->getIt('person')->results();

for($i=0; $i<=count($output); $i++) {
$response['person'][$i] = [];
$response['person'][$i]['fname'] = $output[$i]['fname'];
$response['person'][$i]['lname'] = $output[$i]['lname'];
}

 print_r(json_encode($response, JSON_PRETTY_PRINT));
解决方案1: 在这一行之后:

for($i=0; $i<=count($output); $i++) {
if(!array_key_exists($i, $output)) continue;
解决方案2: 只需更换
解决方案1:
在这一行之后:

for($i=0; $i<=count($output); $i++) {
if(!array_key_exists($i, $output)) continue;
解决方案2:
只需替换您收到的通知,因为偏移量2实际上不存在。您查找偏移量2的唯一原因是因为循环条件
您得到了通知,因为偏移量2实际上不存在。查找偏移量2的唯一原因是因为循环条件
可能我错了,但是循环继续条件不应该使用<运算符吗?可能我错了,但是循环继续条件不应该使用<运算符吗?谢谢您的解释,示例不要慌张,非常感谢您提供的有用信息和另一个有效的答案。感谢您的解释和示例不要惊慌,非常感谢您提供的有用信息和另一个有效的答案。
foreach ($output as $person) {
    $response['person'][] = ['fname' => $output['fname'], 'lname' => $output['lname']];
}