Home > Article > Backend Development > PHP traverses multidimensional array to change the value of the array
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'; } }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'; } }
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.