PHP/Data Structure/array merge — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 10:37, 26 мая 2010
Содержание
- 1 Appending One Array to Another
- 2 array_merge
- 3 array_merge() and + operator
- 4 array_merge() example
- 5 array_merge( ) function combines two or more arrays by renumbering numerical indexes and overwriting string indexes
- 6 array_merge() function merges 1 to N arrays together
- 7 array_merge( ) retains array keys when possible.
- 8 obtaining the union of two arrays
- 9 Using array_merge() and the + operator with associative arrays as well
Appending One Array to Another
<?php
function array_display($array, $pre=FALSE){
$tag = $pre ? "pre" : "p";
printf("<%s>%s</%s>\n", $tag, var_export($array, TRUE), $tag);
}
$arr1 = array(1, 2, 3);
$arr2 = array(10, 20, 30);
$arr3 = array(5, 10, 15, 20);
$comb1 = array_merge($arr1, $arr2);
$comb2 = array_merge($arr2, $arr1);
$comb3 = array_merge($arr3, $arr2, $arr1);
array_display($comb1);
array_display($comb2);
array_display($comb3);
?>
<?php
$arr4 = array(10 => "a", 11 => "b", 12 => "c");
array_display(array_merge($arr1, $arr4), TRUE);
array_display($arr1 + $arr4, TRUE);
?>
<?php
$arr5 = array(1 => "x", 2 => "y", 3 => "z");
array_display(array_merge($arr1, $arr5), TRUE);
array_display($arr1 + $arr5, TRUE);
?>
<?php
$dogs1 = array( "L" => "C", "B" => "S","R" => "A");
$dogs2 = array("R" => "D", "T" => "S");
array_display(array_merge($dogs1, $dogs2), TRUE);
array_display($dogs1 + $dogs2, TRUE);
?>
array_merge
<?php
$face = array("J","Q","K","A");
$numbered = array("2","3","4","5","6","7","8","9");
$cards = array_merge($face, $numbered);
shuffle($cards);
print_r($cards);
?>
array_merge() and + operator
<?php
function array_display($array, $pre=FALSE) {
$tag = $pre ? "pre" : "p";
printf("<%s>%s</%s>\n", $tag, var_export($array, TRUE), $tag);
}
$arr4 = array(10 => "a", 11 => "b", 12 => "c");
$arr1 = array(1, 2, 3);
array_display(array_merge($arr1, $arr4), TRUE);
array_display($arr1 + $arr4, TRUE);
?>
array_merge() example
<?
$array1 = array ("color" => "red", 2, 4);
$array2 = array ("a", "b", "color" => "green", "shape" => "trapezoid");
array_merge ($array1, $array2);
?>
array_merge( ) function combines two or more arrays by renumbering numerical indexes and overwriting string indexes
array array_merge ( array arr1 [, array arr2 [, array ...]] )
<?
$toppings1 = array("Pepperoni", "Cheese", "Anchovies", "Tomatoes");
$toppings2 = array("Ham", "Cheese", "Peppers");
$both_toppings = array_merge($toppings1, $toppings2);
var_dump($both_toppings);
?>
array_merge() function merges 1 to N arrays together
The function"s syntax is: array array_merge (array array1, array array2, . . ., array arrayN)
<?
$arr_1 = array ("s", "g", "l");
$arr_2 = array ("b", "c", "l");
$arr_3 = array ("p", "o");
$arr_4 = array_merge ($arr_2, $arr_1, $arr_3);
print_r($arr_4);
?>
array_merge( ) retains array keys when possible.
<?
$arr1 = array("Paul"=>25, "I"=>38, "Nick"=>27);
$arr2 = array("I"=>27, "Paul"=>38);
print "Merge:\n";
var_dump(array_merge($arr1, $arr2));
?>
obtaining the union of two arrays
<?php
function array_display($array, $pre=FALSE) {
$tag = $pre ? "pre" : "p";
printf("<%s>%s</%s>\n", $tag, var_export($array, TRUE), $tag);
}
$arr5 = array(1 => "x", 2 => "y", 3 => "z");
$arr1 = array(1 => "x", 2 => "y", 3 => "z");
array_display(array_merge($arr1, $arr5), TRUE);
array_display($arr1 + $arr5, TRUE);
?>
Using array_merge() and the + operator with associative arrays as well
<?php
function array_display($array, $pre=FALSE) {
$tag = $pre ? "pre" : "p";
printf("<%s>%s</%s>\n", $tag, var_export($array, TRUE), $tag);
}
$dogs1 = array("A" => "C", "B" => "C", "D" => "C");
$dogs2 = array("a" => "a", "b" => "b");
array_display(array_merge($dogs1, $dogs2), TRUE);
array_display($dogs1 + $dogs2, TRUE);
?>