array("blackberry""/> array("blackberry"">
Home > Article > Backend Development > Share some interesting array methods
Recursively replace elements of the first array using the passed array 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);
With index check Calculate the intersection of arrays and use the callback function to compare the indicesarray_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"));
Recursively merge one or more arrays array_merge_recursive
$ar1 = array("color" => array("favorite" => "red"), 5);\ $ar2 = array(10, "color" => array("favorite" => "green", "blue"));\ $result = array_merge_recursive($ar1, $ar2);
Recursively replace the elements of the first array using the passed arrayarray_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);\
Convert a linear array to a tree, or multidimensional array
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));
The above is the detailed content of Share some interesting array methods. For more information, please follow other related articles on the PHP Chinese website!