Home >Backend Development >PHP Tutorial >Solution to 'Cannot create references to elements of...' reported by php foreach

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

一个新手
一个新手Original
2017-09-14 09:21:091314browse

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!

Statement:
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