Solution to 'Cannot create references to elements of...' reported by php foreach

一个新手
Release: 2023-03-16 06:36:02
Original
1245 people have browsed it

Today, when I used PHP foreach database to query the results in the project, for the sake of convenience, I did not judge whether the data existed. When I directly used (array) to force-convert the data, the web page could not be opened at first, and it reported 502. I was confused, and suddenly PHP reported "Cannot create references to elements of a temporary array expression" and found the error line. The original code is as follows:


foreach ((array) $net_arr as $k => &$val) {    
    $network[$val['node_ip']][$val['ifname']] = $val;
}
Copy after login

Later, it was changed to the following code and everything returned to normal.


if (!empty($net_arr)) {    
    foreach ($net_arr as $k => &$val) {        
    $network[$val['node_ip']][$val['ifname']] = $val;
    }
}
Copy after login

The above is the detailed content of Solution to 'Cannot create references to elements of...' reported by php foreach. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!