public function erp_data(){
$conditions = [
'status' => 1,
'print_status'=> '未打印'
];
$data = $this->Unpaid->find('all', [
'conditions' => $conditions
]);
if ($data === false) {
$res = array(
"code" => 400,
"msg" => "获取数据失败!",
'data' => ''
);
} elseif (empty($data)) {
$res = array(
"code" => 0,
"msg" => "数据为空!",
'data' => ''
);
}else{
$Unpaid = array();
foreach($data as $v){
$v = array_reduce(array_values($v),'array_merge',[]);
$v['title'] = CORPORATE_NAME;
$Unpaid[] = $v;
}
$res = array(
"code" => 200,
"msg" => "获取款数据成功!",
'data' => $data
);
foreach($data as &$value){
$value['print_status'] = '已打印';
$value['update_time'] = date('Y-m-d H:i:s');
}
// $this->Unpaid->saveAll($data);
}
echo json_encode($res);
exit;
}
}
// 解决办法
<?php
$arr = [1,2,3,4,5];
foreach ($arr as $key => &$value) {
}
foreach ($arr as $key => $value) {
}
var_dump($arr);
//预期结果
array(5) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
}
// 实际结果
array(5) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
&int(4)
}
// 解决办法 使用unset
<?php
$arr = [1,2,3,4,5];
foreach ($arr as $key => &$value) {
}
unset($value);
foreach ($arr as $key => $value) {
}
var_dump($arr);
PHP foreach 循环时 & 出现的错误
于 2023-10-18 18:23:10 首次发布