Home >Backend Development >PHP Tutorial >php轮换数组内元素

php轮换数组内元素

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 11:54:291120browse

php替换数组内元素

php中虽然处理数组的函数特别多,但是有的函数还要我们自己封装,譬如替换数组里面的元素:

下面是替换数组的键=>KEY值:

<?php $arr = array();$array = array(    0 => array(shop_name=>1,shop_id=>2),    1 => array(shop_name => 2, shop_id=>3));for($i=0; $i<count>';print_r ($arr);echo '</count>
';?>

下面实例是封装的一个方法:

开发小技巧实例程序,使用递归替换数组中的内容。

把数组内的替换成{ }。

你可以根据实际情况去使用这个函数。替换数组内的元素还是很方便的;

<?php $arr = array("<小刚>","","","","");    function arrContentReplact($array)    {        if(is_array($array))        {            foreach($array as $k => $v)            {                $array[$k] = arrContentReplact($array[$k]);            }        }else        {            $array = str_replace(array(''), array('{', '}'), $array);        }        return $array;    }       $arr3 = arrContentReplact($arr);       echo "<pre class="brush:php;toolbar:false">";    print_r($arr3);    echo "
";?>

以上例子输出的结果为:

Array(    [0] => {小刚}    [1] => {小晓}    [2] => {小飞}    [3] => {小李}    [4] => {小红})

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