Home >Backend Development >PHP Tutorial >Solution to 'Cannot create references to elements of...' reported by php foreach
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; }
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; } }
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!