分享几个有意思的数组方法

【虚拟资源买卖网www.cnused.com】源码虚拟资源交易平台,免费开通VIP商户!
吆喝资源网官方客服QQ:787749153官方唯一QQ号码
文字广告位招租      好位置不等人!200元/月

使用传递的数组递归替换第一个数组的元素 array_replace_recursive

$base = array('citrus' => array( "orange") , 'berries' => array("blackberry", "raspberry"), );
$replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry'));
$basket = array_replace_recursive($base, $replacements);

537c289eff18d236102fd1c200c9148.png

带索引检查计算数组的交集,用回调函数比较索引 array_intersect_uassoc

$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
print_r(array_intersect_uassoc($array1, $array2, "strcasecmp"));

738c9dcb2e3ecc4a206089324fd5d0d.png

递归地合并一个或多个数组 array_merge_recursive

$ar1 = array("color" => array("favorite" => "red"), 5);/
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));/
$result = array_merge_recursive($ar1, $ar2);

2f97f2519f7d77e78ba5b80314dd1bf.png

使用传递的数组递归替换第一个数组的元素 array_replace_recursive

$base = array('citrus' => array( "orange") , 'berries' => array("blackberry", "raspberry"), );
$replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry'));
$basket = array_replace_recursive($base, $replacements);
print_r($basket);/

将一个线性数组转换为一个树,或者多维数组

function array_stack (&$a, $p = '@parent', $c = '@children')
    {
      $l = $t = array();
      foreach ($a AS $key => $val):
        if (!$val[$p]) $t[$key] =& $l[$key];
        else $l[$val[$p]][$c][$key] =& $l[$key];
        $l[$key] = (array)$l[$key] + $val;
      endforeach;
      return $a = array('tree' => $t, 'leaf' => $l);
    }
    $node = array();
    $node[1] = array('@parent' => 0, 'title' => 'I am node 1.');
    $node[2] = array('@parent' => 1, 'title' => 'I am node 2.');
    $node[3] = array('@parent' => 2, 'title' => 'I am node 3.');
    $node[4] = array('@parent' => 1, 'title' => 'I am node 4.');
    $node[5] = array('@parent' => 4, 'title' => 'I am node 5.');
    print_r(array_stack($node));

以上就是分享几个有意思的数组方法的详细内容,更多请关注吆喝资源网其它相关文章!

本文转自PHP中文网,吆喝网(http://yaoohe.com)搜集整理,如有疑问请联系本站客服!

本文来源网络,其内容均为发表者提供,文章内容系作者个人观点,不代表本站对观点赞同或支持。如需有侵权,请在7日内联系本站客服进行处理,否则视为放弃追究权利,客服QQ:787749153,请注明。
1

发表评论