|
![](icon/private_name/1.gif) |
8³â Àü |
function pre($val){
echo '<pre>';
print_r($val);
echo '</pre>';
}
/*
¹è¿»ý¼º
array
array = array(mixed)
http://php.net/manual/en/function.array.php
*/
$arr = array(1,2,3,4,5,6);
pre($arr);
$arr = array('a'=>'apple','b'=>'banana','c'=>'cherry');
pre($arr);
$arr = array('one',5=>'two','three');
pre($arr);
$arr = array(1,3,2=>8,4,0=>6);
pre($arr);
$arr = array('a'=>'apple','b'=>'banana','c'=>'cherry','d'=>array('do'=>array('dol'=>'dolphin','dou'=>'double'),'dr'=>'dragon','du'=>'duck','dw'=>'dwarf'));
pre($arr);
/*
¹è¿ÀÇ ¿ø¼Ò¸¦ º¯¼ö¿¡ ÇÒ´ç
list
array = list(mixed)
http://php.net/manual/en/function.list.php
*/
$alphabet = array('apple','banana','coffee','dance','elf');
list($red,$yellow,$brown,,$game) = $alphabet;
echo $game;
/*
¹è¿¿¡¼ Æ÷ÀÎÅÍ°¡ °¡¸®Å°´Â ¿ø¼ÒÀÇ Å°¿Í °ªÀ» °¡Á®¿Í ¹è¿·Î ¹Ýȯ
each
array = each(array_&$array)
http://php.net/manual/en/function.each.php
*/
$food = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry', 'd'=>'dragon', 'e'=>'egg', 'f'=>'fish');
pre(each($food));
reset($food);
while (list($key, $val) = each($food)) {
echo "$key => $val" . "
";
}
/*
¿¬°ü¹è¿·ÎºÎÅÍ ÇöÀç Æ÷ÀÎÅÍ°¡ °¡¸®Å°´Â ¿ø¼ÒÀÇ Å°¸¦ ¹Ýȯ
key
mixed = key(array_&$array)
http://php.net/manual/en/function.key.php
*/
$food = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry', 'd'=>'dragon', 'e'=>'egg', 'f'=>'fish');
while ($fruit_name = current($food)) {
if ($fruit_name == 'banana') {
echo key($food).'
';
}
next($food);
}
/*
¹è¿·ÎºÎÅÍ ÇöÀç Æ÷ÀÎÅÍ°¡ °¡¸®Å°´Â ¿ø¼ÒÀÇ °ªÀ» ¹Ýȯ
current, pos
mixed = current(array_&$array)
http://php.net/manual/en/function.current.php
http://php.net/manual/en/function.pos.php
*/
$food = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry', 'd'=>'dragon', 'e'=>'egg', 'f'=>'fish');
var_dump(current($food));
/*
¹è¿ÀÇ Æ÷ÀÎÅ͸¦ ´ÙÀ½ ¿ø¼Ò·Î À̵¿
next
mixed = next(array_&$array)
http://php.net/manual/en/function.next.php
*/
$food = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry', 'd'=>'dragon', 'e'=>'egg', 'f'=>'fish');
var_dump(next($food));
/*
¹è¿ÀÇ Æ÷ÀÎÅ͸¦ óÀ½À¸·Î À̵¿
reset
mixed = reset(array_&$array)
http://php.net/manual/en/function.reset.php
*/
$food = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry', 'd'=>'dragon', 'e'=>'egg', 'f'=>'fish');
var_dump(reset($food));
/*
¹è¿ÀÇ Æ÷ÀÎÅ͸¦ ÀÌÀü ¿ø¼Ò·Î À̵¿
prev
mixed = prev(array_&$array)
http://php.net/manual/en/function.prev.php
*/
$food = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry', 'd'=>'dragon', 'e'=>'egg', 'f'=>'fish');
var_dump(prev($food));
/*
¹è¿ÀÇ Æ÷ÀÎÅ͸¦ ³¡À¸·Î À̵¿
end
mixed = end(array_&$array)
http://php.net/manual/en/function.end.php
*/
$food = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry', 'd'=>'dragon', 'e'=>'egg', 'f'=>'fish');
var_dump(end($food));
/*
¹è¿¿¡ °ªÀÌ Á¸ÀçÇÏ´ÂÁö È®ÀÎ
in_array
bool = in_array(mixed_needle, array_haystack)
http://php.net/manual/en/function.in-array.php
*/
$food = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry', 'd'=>'dragon', 'e'=>'egg', 'f'=>'fish', 'g'=>array('ginger','grape'));
if (in_array("banana", $food)) {
echo "Got Banana";
}
if (in_array(array('ginger','grape'), $food, true)) {
echo "Got G Food";
}
/*
¹è¿ÀÌ °¡Áø ¿ø¼ÒÀÇ °¹¼ö¸¦ ¹Ýȯ
count, sizeof
int = count(mixed_array_or_countable)
http://php.net/manual/en/function.count.php
http://php.net/manual/en/function.sizeof.php
*/
$food = array('apple', 'banana', 'c'=>'cherry', 'd'=>'dragon', 'e'=>'egg', 'f'=>'fish', 'g'=>array('gi'=>'ginger','gr'=>'grape'));
echo count($food, COUNT_RECURSIVE); // 9
echo sizeof($food); // 7
for ($i = 0; $i < count($food); $i++)
{
echo $food[$i];
}
/*
ÀÎÀÚ·Î ÁöÁ¤ÇÑ ¿µ¿ªÀÇ ¹®ÀÚ ¶Ç´Â ¼ýÀÚ·Î ±¸¼ºµÈ ¹è¿À» »ý¼º
range
array = range(mixed_start, mixed_end)
http://php.net/manual/en/function.range.php
*/
$arr = range(0,10);
$arr = range(0,100,2);
$arr = range('a','h');
$arr = range('z','o');
foreach ($arr as &$value)
{
echo $value;
}
/*
¹è¿¾È ¿ø¼ÒµéÀÇ ¼ø¼¸¦ ¼¯´Â´Ù
shuffle
bool = shuffle(array_&array)
http://php.net/manual/en/function.shuffle.php
*/
$numbers = range(1, 20);
shuffle($numbers);
foreach ($numbers as $number) {
echo "$number / ";
}
/*
¹è¿À» Á¤·Ä
sort
bool = sort(array_&array)
http://php.net/manual/en/function.sort.php
SORT_REGULAR
SORT_NUMERIC
SORT_STRING
SORT_LOCALE_STRING
SORT_NATURAL
SORT_FLAG_CASE
*/
$food = array('pizza', 'banana', 'cake', 'apple', 'grape', 'fish', 'egg', 100, 200, 101, 10, 22);
sort($food, SORT_NATURAL | SORT_FLAG_CASE);
foreach ($food as $key => $val) {
echo "food[" . $key . "] = " . $val;
}
/*
¹è¿ÀÇ Å°¸¦ ±âÁØÀ¸·Î Á¤·Ä
ksort
bool = ksort(array_&array)
http://php.net/manual/en/function.ksort.php
*/
$fruits = array('d'=>'lemon', 'a'=>'orange', 'c'=>'banana', 'b'=>'apple');
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val ";
}
/*
¹è¿ÀÇ Å°¸¦ À¯ÁöÇÑä Á¤·Ä
asort
bool = asort(array_&array)
http://php.net/manual/en/function.asort.php
*/
$number = array(123,321,88,23,11,15);
asort($number);
foreach ($number as $key => $val) {
echo "\$number[$key] = $val ";
}
/*
¹è¿À» °Å²Ù·Î Á¤·Ä
rsort
bool = rsort(array_&array)
http://php.net/manual/en/function.rsort.php
*/
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
rsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val ";
}
/*
¹è¿ÀÇ Å°¸¦ À¯ÁöÇÑä °Å²Ù·Î Á¤·Ä
arsort
bool = arsort(array_&array)
http://php.net/manual/en/function.arsort.php
*/
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
arsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val ";
}
/*
¹è¿ÀÇ Å°¸¦ ±âÁØÀ¸·Î °Å²Ù·Î Á¤·Ä
krsort
bool = krsort(array_&array)
http://php.net/manual/en/function.krsort.php
*/
$fruits = array("a"=>"lemon", "d"=>"orange", "c"=>"banana", "b"=>"apple");
krsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val ";
}
/*
¾Ë°í¸®ÁòÀ» ÀÌ¿ëÇÏ¿© ¹è¿À» Á¤·Ä
natsort
bool = natsort(array_&array)
http://php.net/manual/en/function.natsort.php
*/
$array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png");
asort($array1);
echo "Standard sorting";
print_r($array1); // img1 img10 img12 img2
natsort($array2);
echo "Natural order sorting";
print_r($array2); // img1 img2 img10 img12
echo "Negative numbers";
$negative = array('-5','3','-2','0','-1000','9','1');
print_r($negative);
natsort($negative);
print_r($negative); // -2 -5 -1000 0 1 3 9
echo "Zero padding";
$zeros = array('09', '8', '10', '009', '011', '0');
print_r($zeros);
natsort($zeros);
print_r($zeros); // 0 8 009 09 10 011
echo "Other characters interfering";
$images_oops = array('image_1.jpg','image_12.jpg', 'image_21.jpg', 'image_4.jpg');
print_r($images_oops);
natsort($images_oops);
print_r($images_oops); // 1 4 12 21
echo "Sort by keys";
$smoothie = array('orange' => 1, 'apple' => 1, 'yogurt' => 4, 'banana' => 4);
print_r($smoothie);
uksort( $smoothie, 'strnatcmp');
print_r($smoothie); // apple 1 banana 4 orange 1 yogurt 4
/*
¾Ë°í¸®ÁòÀ» ÀÌ¿ëÇÏ¿© ´ë¼Ò¹®ÀÚ ±¸ºÐ¾øÀÌ ¹è¿À» Á¤·Ä
natcasesort
bool = natcasesort(array_&array)
http://php.net/manual/en/function.natcasesort.php
*/
$array1 = $array2 = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');
sort($array1);
echo "Standard sorting";
print_r($array1); // IMG0 IMG3 img1 img10 img12 img2
natcasesort($array2);
echo "Natural order sorting (case-insensitive)";
print_r($array2); // IMG0 img1 img2 IMG3 img10 img12
/*
¹è¿¿¡¼ ¸Ç³¡ ¿ø¼Ò¸¦ Á¦°Å
array_pop
mixed = array_pop(array_&array)
http://php.net/manual/en/function.array-pop.php
*/
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
pre($stack);
/*
¹è¿ÀÇ ¸Ç³¡¿¡ ¿ø¼Ò¸¦ Ãß°¡
array_push
int = array_push(array_&array, mixed_value1, ...)
http://php.net/manual/en/function.array-push.php
*/
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
pre($stack);
/*
¹è¿¿¡¼ ¸Ç¾Õ ¿ø¼Ò¸¦ Á¦°Å
array_shift
mixed = array_shift(array_&array)
http://php.net/manual/en/function.array-shift.php
*/
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
pre($stack);
/*
¹è¿ÀÇ ¸Ç¾Õ¿¡ ¿ø¼Ò¸¦ Ãß°¡
array_unshift
int = array_unshift(array_&array, mixed_value1, ...)
http://php.net/manual/en/function.array-unshift.php
*/
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
pre($queue);
/*
¹è¿¿¡¼ Áߺ¹µÈ ¿ø¼Ò¸¦ Á¦°Å
array_unique
array = array_unique(array_array)
http://php.net/manual/en/function.array-unique.php
*/
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
pre($result); // a green 0 red 1 blue
$input = array(4, "4", "3", 4, 3, "3");
$result = array_unique($input);
var_dump($result); // array(2) { [0]=> int(4) [2]=> string(1) "3" }
/*
¹è¿ÀÇ À妽º¸¦ ¹Ýȯ
array_values
array = array_values(array_array)
http://php.net/manual/en/function.array-values.php
*/
$array = array("size" => "XL", "color" => "gold");
pre(array_values($array)); // 0 XL 1 gold
/*
¹è¿ÀÇ Å°¸¦ ¹Ýȯ
array_keys
array = array_keys(array_array, mixed_search_value)
http://php.net/manual/en/function.array-keys.php
*/
$array = array(0 => 100, "color" => "red");
pre(array_keys($array)); // 0 0 1 color
$array = array("blue", "red", "green", "blue", "blue");
pre(array_keys($array, "blue")); // 0 0 1 3 2 4 blue ¿¡ ´ëÇÑ Å°¸¸ ¹Ýȯ
$array = array("color" => array("blue", "red", "green"),
"size" => array("small", "medium", "large"));
pre(array_keys($array)); // 0 color 1 size
/*
¹è¿ÀÇ ¿ø¼Ò¸¦ ¿ª¼øÀ¸·Î Á¤·ÄÇÏ¿© ¹Ýȯ
array_reverse
array = array_reverse(array_array)
http://php.net/manual/en/function.array-reverse.php
*/
$input = array("php", 4.0, array("green", "red"));
$reversed = array_reverse($input);
$preserved = array_reverse($input, true);
// true Çϸé Å°¸¦ º¸Á¸ÇÏ¸é¼ ¿ª¼øÀ¸·Î ¹Ýȯ
pre($input);
pre($reversed);
pre($preserved);
/*
¹è¿¿¡¼ Çϳª ÀÌ»ó ¿ø¼Ò¸¦ ·£´ýÇÏ°Ô ÃßÃâÇÏ¿© ¹Ýȯ
array_rand
mixed = array_rand(array_array, int_numreq)
http://php.net/manual/en/function.array-rand.php
*/
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
// 2 °¡Á®¿Ã ¿ø¼Ò ¼ö, ±âº»°ªÀº 1
echo $input[$rand_keys[0]];
echo $input[$rand_keys[1]];
/*
ÁöÁ¤ÇÑ ±æÀ̸¸Å ƯÁ¤ °ªÀ¸·Î ¹è¿ ä¿ì±â
array_pad
array = array_pad(array_array, int_size, mixed_value)
http://php.net/manual/en/function.array-pad.php
*/
$input = array(12, 10, 9);
$result = array_pad($input, 5, 0);
pre($result); // 12, 10, 9, 0, 0
$result = array_pad($input, -7, -1);
pre($result); // -1, -1, -1, -1, 12, 10, 9
$result = array_pad($input, 2, "noop");
pre($result); // 12 10 9
/*
ƯÁ¤°ªÀ¸·Î ¹è¿Ã¤¿ì±â
array_fill
array = array_fill(int_start_index, int_num)
http://php.net/manual/en/function.array-fill.php
*/
$a = array_fill(5, 6, 'banana');
$b = array_fill(-2, 3, 'pear');
pre($a);
pre($b);
/*
¹è¿ÀÇ °¢ ¿ø¼Ò¿¡ ¹Ì¸® Á¤ÀÇÇÑ ÇÔ¼ö¸¦ Àû¿ë
array_walk
bool = array_walk(array_&array, callable_callback)
http://php.net/manual/en/function.array-walk.php
*/
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
function test_alter(&$item1, $key, $prefix)
{
$item1 = "$prefix: $item1";
}
function test_print($item2, $key)
{
echo "$key. $item2";
}
echo "Before ...:";
array_walk($fruits, test_print);
echo "... and after:";
array_walk($fruits, test_alter, 'fruit');
array_walk($fruits, test_print);
/*
¹è¿ÀÇ °¢ ¿ø¼Ò¿¡ ¹Ì¸® Á¤ÀÇÇÑ ÇÔ¼ö¸¦ Àû¿ë. À̶§ ¹è¿ÀÇ ¿ø¼Ò°¡ ¶Ç ´Ù¸¥ ¹è¿À» Æ÷ÇÔÇÑ °æ¿ì¿¡´Â ÇØ´ç ¹è¿¿¡ ´ëÇؼµµ ÁöÁ¤ÇÑ ÇÔ¼ö¸¦ È£Ãâ
array_walk_recursive
bool = array_walk_recursive(array_&array, callable_callback)
http://php.net/manual/en/function.array-walk-recursive.php
*/
$sweet = array('a' => 'apple', 'b' => 'banana');
$fruits = array('sweet' => $sweet, 'sour' => 'lemon');
function test_print($item, $key)
{
echo "$key holds $item
";
}
array_walk_recursive($fruits, test_print);
/*
¿©·¯°³ÀÇ ¹è¿À» ÇϳªÀÇ ¹è¿·Î ÇÕħ
array_merge
array = array_merge(array_array1, array_array2, ...)
http://php.net/manual/en/function.array-merge.php
*/
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
//Å°°¡ ¼ýÀÚÀÏ °æ¿ì ´Ù¸¥ ¼ýÀÚ·Î ÀçºÎ¿© µÈ´Ù
//º¯¼ö+¹è¿µµ °¡´É array_merge((array)"foo", $array1, $array2);
pre($result);
/*
¿©·¯°³ÀÇ ¹è¿À» ÇϳªÀÇ ¹è¿·Î ÇÕÄ¡µÇ ¹®ÀÚ¿ Å°°¡ µ¿ÀÏÇÑ ¿ø¼Ò°¡ ¿©·¯°³ Á¸ÀçÇÏ´Â °æ¿ì À̵éÅ°¸¦ °®´Â ¸ðµç ¿ø¼Ò¸¦ ¹è¿¿¡ ÇÔ²² º´ÇÕ
array_merge_recursive
array = array_merge_recursive(array_array1, array_array2, ...)
http://php.net/manual/en/function.array-merge-recursive.php
*/
$array1 = array("color" => array("favorite" => "red"), 5);
$array2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($array1, $array2);
pre($result); // red ¿Í green ÀÌ favorite ¹è¿¿¡ ÇÔ²² µé¾î°¨
/*
´Ù¸¥ ¹è¿¿¡´Â ¾ø´Â ¿ø¼Ò¸¸À¸·Î ±¸¼ºµÈ ¹è¿À» ¹Ýȯ
array_diff
array = array_diff(array_array1, array_array2, ...)
http://php.net/manual/en/function.array-diff.php
*/
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
pre($result); // 1 blue
/*
´Ù¸¥ ¹è¿¿¡´Â ¾ø´Â ¿ø¼Ò¸¸À¸·Î ±¸¼ºµÈ ¹è¿À» ¹Ýȯ. Å°¿Í °ªÀÌ ¸ðµÎ ÀÏÄ¡ÇÏ´Â ¿ø¼Ò¸¸À» Á¦°ÅÇÑ ³ª¸ÓÁö ¿ø¼Ò¸¦ ¹è¿·Î ¹Ýȯ
array_diff_assoc
array = array_diff_assoc(array_array1, array_array2, ...)
http://php.net/manual/en/function.array-diff-assoc.php
*/
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
pre($result); // b brown c blue 0 red
/*
´Ù¸¥ ¹è¿¿¡´Â ¾ø´Â Å°¸¦ °®´Â ¿ø¼Ò¸¸À¸·Î ±¸¼ºµÈ ¹è¿À» ¹Ýȯ
array_diff_key
array = array_diff_key(array_array1, array_array2, ...)
http://php.net/manual/en/function.array-diff-key.php
*/
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
pre(array_diff_key($array1, $array2)); // red 2 purple 4
/*
´Ù¸¥ ¸ðµç ¹è¿¿¡µµ Á¸ÀçÇÏ´Â ¿ø¼Ò¸¸À¸·Î ±¸¼ºµÈ ¹è¿À» ¹Ýȯ
array_intersect
array = array_intersect(array_array1, array_array2, ...)
http://php.net/manual/en/function.array-intersect.php
*/
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
pre($result); // a green 0 red
/*
Å°¿Í °ªÀÌ ¸ðµÎ ÀÏÄ¡ÇÏ´Â ¿ø¼ÒÁß¿¡¼ ¸ðµç ¹è¿¿¡ Á¸ÀçÇÏ´Â ¿ø¼Ò¸¸À¸·Î ±¸¼ºµÈ ¹è¿À» ¹Ýȯ
array_intersect_assoc
array = array_intersect_assoc(array_array1, array_array2, ...)
http://php.net/manual/en/function.array-intersect-assoc.php
*/
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "b" => "yellow", "blue", "red");
$result_array = array_intersect_assoc($array1, $array2);
pre($result_array); //a green
/*
¹è¿¿¡ ÁöÁ¤ÇÑ À̸§ÀÇ Å°¸¦ °®´Â ¿ø¼Ò°¡ Á¸ÀçÇÏ´ÂÁö °Ë»ç
array_key_exists
bool = array_key_exists(mixed_key, array_array)
http://php.net/manual/en/function.array-key-exists.php
*/
$search_array = array('first' => null, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
isset($search_array['first']); // false , NULL À̹ǷÎ
array_key_exists('first', $search_array); // true
/*
ÁÖ¾îÁø °ªÀ¸·Î ¹è¿À» °Ë»çÇÏ°í ¸¸ÀÏ Á¸ÀçÇϸé ÇØ´ç ¿ø¼ÒÀÇ Å°¸¦ ¹Ýȯ
array_search
mixed = array_search(mixed_needle, array_haystack)
http://php.net/manual/en/function.array-search.php
*/
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // 2;
$key = array_search('red', $array); // 1;
/*
¹è¿ÀÇ ¸ðµç ¹®ÀÚ¿ Å°¸¦ ´ë¹®ÀÚ ¶Ç´Â ¼Ò¹®ÀÚ·Î ¹Ù²Þ
array_change_key_case
array = array_change_key_case(array_array, int_case)
http://php.net/manual/en/function.array-change-key-case.php
CASE_UPPER
CASE_LOWER
*/
$input_array = array("FirSt" => 1, "SecOnd" => 3);
pre(array_change_key_case($input_array, CASE_UPPER));
/*
¹è¿ÀÇ °¢ ¿ø¼Ò¿¡ »ç¿ëÀÚ°¡ Á¤ÀÇÇÑ ÇÔ¼ö¸¦ Àû¿ëÇÏ°í Àû¿ëÇÑ ÈÄÀÇ °á°ú¸¦ ¹è¿·Î ¹Ýȯ
array_map
array = array_map(callable_callback, array_array1, ...)
http://php.net/manual/en/function.array-map.php
*/
// 1 8 27 64 125
function cube($n)
{
return($n * $n * $n);
}
$a = array(1, 2, 3, 4, 5);
$b = array_map(cube, $a);
pre($b);
// 2 4 6 8 10
$func = function($value) {
return $value * 2;
};
pre(array_map($func, range(1, 5)));
// uno dos tres cuatro cinco
function show_Spanish($n, $m)
{
return("The number $n is called $m in Spanish");
}
function map_Spanish($n, $m)
{
return(array($n => $m));
}
$a = array(1, 2, 3, 4, 5);
$b = array("uno", "dos", "tres", "cuatro", "cinco");
$c = array_map(show_Spanish, $a, $b);
pre($c);
$d = array_map(map_Spanish, $a , $b);
pre($d);
// 1 one uno, 2 two dos, 3 three tres, ...
$a = array(1, 2, 3, 4, 5);
$b = array("one", "two", "three", "four", "five");
$c = array("uno", "dos", "tres", "cuatro", "cinco");
$d = array_map(null, $a, $b, $c);
pre($d);
// stringkey value, 0 value
$arr = array("stringkey" => "value");
function cb1($a) {
return array ($a);
}
function cb2($a, $b) {
return array ($a, $b);
}
pre(array_map(cb1, $arr));
pre(array_map(cb2, $arr, $arr));
pre(array_map(null, $arr));
pre(array_map(null, $arr, $arr));
/*
¹è¿¿¡ ÀÖ´Â ¿ø¼ÒÀÇ ÇÕÀ» °è»êÇÏ¿© ¹Ýȯ
array_sum
number = array_sum(array_array)
http://php.net/manual/en/function.array-sum.php
*/
$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a); // 20
$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
echo "sum(b) = " . array_sum($b); //6.9 |
|
̵̧ : 341 |
̵̧
¸ñ·Ï
|
|