PHP traverses multidimensional array to change the value of the array

WBOY
Release: 2016-08-08 09:21:40
Original
1092 people have browsed it

One of the problems in today’s project is that the found results need to be filtered again according to the conditions. Because what is found is a two-dimensional array, the array is traversed directly. The two-dimensional array I use is $list. First traverse it like this:

foreach($list as $k=>$v){
            if(strpos($v['distance'],'7.') === 0 &&strrchr($v['distance'],'km')== 'km' ){
                $v['distance'] = '7.0km';
            }
        }
Copy after login
But $list does not change after doing it like this. The reason is that $V is not an element in $list. It is just the same as $ in $List. ['$k'] A one-dimensional array with equal key values. Once you know the reason, it will be easier to handle. The correct code is as follows:
foreach($list as $k=>$v){
            if(strpos($v['distance'],'7.') === 0 &&strrchr($v['distance'],'km')== 'km' ){
                $list[$k]['distance'] = '7.0km';
            }
        }
Copy after login

This will really change $List.

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces PHP to traverse multi-dimensional arrays and change the values ​​of the arrays, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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!