0% found this document useful (0 votes)
624 views

PHP Array Function

This document provides descriptions and examples of various PHP array functions. It covers 45 different functions including: - Array() for creating arrays - Array_walk() for applying a user-defined function to every element of an array - Arsort() and asort() for sorting array elements in descending and ascending order respectively - Functions for filtering, flipping, intersecting, merging, mapping, searching, shifting, slicing, sorting, and summarizing array elements. Detailed code examples are provided to illustrate the usage of each function and its impact on the array elements. The functions provide a comprehensive set of tools for manipulating PHP arrays in various ways.

Uploaded by

api-20013511
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
624 views

PHP Array Function

This document provides descriptions and examples of various PHP array functions. It covers 45 different functions including: - Array() for creating arrays - Array_walk() for applying a user-defined function to every element of an array - Arsort() and asort() for sorting array elements in descending and ascending order respectively - Functions for filtering, flipping, intersecting, merging, mapping, searching, shifting, slicing, sorting, and summarizing array elements. Detailed code examples are provided to illustrate the usage of each function and its impact on the array elements. The functions provide a comprehensive set of tools for manipulating PHP arrays in various ways.

Uploaded by

api-20013511
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

PHP array function

1. Array() function
Array array(…);
Create arrary
$name = array(“Jollen”, “Paul”, “Ketty”);

$name = array(“one” => “ 1”, “two” => “2”);


Same
$name[“one”] = “1”;
$name[“two”] = “2”;

<?
$fruits = array
(
“fruits” => array(“a”=>”orange”, “b”=>”banana”, “c”=>”apple”);
“numbers” => array(1,2,3,4,5,6);
“holes” => array(“first”, 5 => “second”, “third”);
);
?>
Same
<?
$fruits[“fruits”][“a”] = “orange”;
$fruits[“fruits”][“b”] = “banana”;
$fruits[“fruits”][“c”] = “apple”;
$fruits[“numbers”][0] = 1;
$fruits[“numbers”][1] = 2;
$fruits[“numbers”][2] = 3;
$fruits[“numbers”][3] = 4;
$fruits[“numbers”][4] = 5;
$fruits[“numbers”][5] = 6;
$fruits[“holes”][0] = “first”;
$fruits[“holes”][5] = “second”;
$fruits[“holes”][6] = “third”;
?>
2. Array_walk() function
Int array_walk(array arr, string func);
Example
<?php
$fruits = array(“a”=>”orange”, “b”=>”banana”, “c”=>”apple”);
Function test_alter($item1) {
$iteml = ‘bogus’;
}
Function test_print($item2) {
Echo “$item2”;
}

Array_walk($fruits, ‘test_print’); //print orange banana apple


Array_walk($fruits, ‘test_alter’); /*array change to
$fruits[“a”] = “bogus”;
$fruits[“b”] = “bogus”;
$fruits[“c”] = “bogus”; */
Array_walk($fruits, ‘test_print’); //print bogus bogus bogus
?>

3. Arsort() function
Void arsort(array array); //content sort by z->a
Example
<?
$fruits = array(“d”=>”lemon”, “a”=>”orange”, “b”=>”banana”, “c”=>”apple”);
Arsort($fruits);
For(reset($fruits); $key = key($fruits); next($fruits)) {
Echo “fruits[$key] = “.$fruits[$key].”\n”;
Output:
fruits[a] = orange
Fruits[d] = lemon
Fruits[b] = banana
Fruits[c] = apple

4. Asort() function
Void asort(array array); //content sort by a->z
5. Array_count_values() function
Array array_count_values(array input);
<?php
$arr = array(1, 2, 2, 3, “Hello”, “Hello”, “World”);
$arr_c = array_count_values($arr);
Print $arr_c;
?>
Output:
Array( [1] => 1 [2] => 2 [3] => 1 [Hello] => 2 [World] => 1)

Array_diff() function
Array array_diff(array array1, array array2, array...);
<?
$arr1 = array(“green”, “white”, “red”, “blue”, “green”);
$arr2 = array(“green”, “blue”, “red”);
$result = array_diff($arr1, $arr2); // (set operation) result = $arr1\$arr2
Print_r($result);
?>
Output
Array ( [1] => white )

6. Array_filter() function
Array array_filter(array input [, mixed callback]);
Example
<pre>
<?php
Function filter1($val)
{
Return (strlen($val)) == 5);
}

Function filter2($val)
{
Return (strlen($val) < 5);
}
$arr1 = array (“green”, “white”, “red”, “blue”, “green”);
$arr2 = array (“green”, “blue”, “red”);
$result1 = array_filter($arr1, “filter1”);
$result2 = array_filter($arr2, “filter2”);

Print_r($arr1);
Print_r($arr2);
Print_r($result1);
Print_r($result2);
?>
</pre>

Output:
Array
(
[0] => green
[1] => while
[2] => red
[3] =>blue
[4] =>green
)
Array
(
[0] => green
[1] => blue
[2] => red
)
Array
(
[0] =>green
[1] =>while
[4] =>green
)
Array
(
[1] => blue
[2] => red
)
7. Array_flip() function
<pre>
<?php
$arr = array(“A”=>”a”, “B”=>”b”, “C”=>”c”, “D”=>”c”);
$arr_flip = array_flip($arr);

Print_r($arr);
Print_r($arr_flip);
?>
</pre>

Output:
Array
(
[A] => a
[B] => b
[C] => c
[D] => c
)
Array
(
[a] => A
[b] => B
[c] => D
)

8. Array_intersect() function
Array array_intersect(array array1, array array2 [, array …]);
<?
$arr1 = array(“green”, “white”, “red”, “blue”, “green”);
$arr2 = array(“green”, “blue”, “red”);
$arr3 = array(“red”);

$result1 = array_intersect($arr1, $arr2, $arr3); // $arr1 ∩ $ arr2 ∩ $arr3


$result2 = array_intersect($arr1,$arr2); // $arr1 ∩ $ arr2
?>
9. Array_key_exists() function
Bool array_key_exists(mixed key, array search);
If key is exists in search, it will be return true

10. Array_keys() function


Array array_keys(array input [, mixed search_value]);
Return key of input, if assign search_value, it only return key of search_value

Array_map() function
Array array_map(mixed callback, array arr1 [, array arr2…]);
Example
<?
11. Function cube($val)
{
Return $val*$val*$val;
}
$arr = array(1,3,5,7,9);
$result = array_map(“cube”, $arr);
Print_r($result);
?>
Output:
Array
(
[0] =>1
[1] =>27
[2] =>125
[3] =>343
[4] =>729
)

12. Array_merge() function


Array array_merge(array array1, array array2 [, array array3…]);
合併兩個 array

13. Array_merge_recursive() function


Array array_merge_recursive(array array1, array array2 [, array array3…]);
同 array_merge() function 一樣,但當遇見有兩 key 一樣的時後,會自動產生多一個
array 去放相同 key 的 values
14. Array_multisort() function
bool array_multisort(array ar1 [, mixed arg [, mixed … [, array ….]]]);
Sort type:
SORT_ASC
SORT_DESC
Compare type:
SORT_REGULAR: general compare
SORT_NUMERIC: number compare
SORT_STRING: string compare

15. Array_pad() function


array array_pad(array input, int pad_size, mixed pad_value);
<pre>
<?php
$arr = array(9, 1, 3);
$result1 = array_pad($arr, 5, 0);
$result2 = array_pad($arr,-10,5);
$result3 = array_pad($arr,2,0);
Print_r…….;
?>
Array
(
[0] => 9
[1] => 1
[2] => 3
[3] => 0
[4] => 0
)
Array
(
[0] => 5
[1] => 5
[2] => 5
[3] => 5
[4] => 5
[5] => 5
[6] => 5
[7] => 9
[8] => 1
[9] => 3
)
Array
(
[0] => 9
[1] => 1
[2] => 3
)

16. Array_pop() function


Mixed array_pop(array array);
Get array last value and 減少 array 最後一個元素

17. Array_push() function


Int array_push(array array, mixed var [, mixed …]);
張數個值加入 array 最後的元素

18. Array_rand() function


Mixed array_rand(array input [, int num_req]);
以 random 的方式去取出 array 的 value, 如没值 num_req 則回傳一個數 value

19. Array_reverse() function


Array array_reverse(array array [, bool preserve_keys]);
反序 function, 如 preserve_keys 係 true 則只對 key 做反序動作,如没有設值則以
value 做反序動作

20. Array_reduce() function


Mixed array_reduce(array input, mixed callback [, mixed initial]);
Example
<?php
Function sum($v, $w) {
$v += $w;
Return $v;
}
$a = array(1, 2, 3, 4, 5);
$b = array_reduce($a,”sum”);
$b = array_reduce($a,”sum”,-100);
?>
Output:
b = 15, c = -85

21. Array_shift() function


Mixed array_shift(array input);
傳回 array 第一個元素 value,然而 array 左移一個元素

22. Array_slice() function


Array array_slice(array input, int offset [, int length]);
從 input 第 offset 個位置至 length 回傳給 array
如 Offset 是正數, 則由 input 頭(0)開始計起
如 Offset 是負數, 則由 input 尾(-1)開始計起,倒數第 2 個數為 -2
如 length 没設值, 則回傳 offset 之後所有 values

23. Array_splice() function


Array array_splice(array input, int offset [, int length [, array replacement]]);
………………..

24. Array_sum() function


Mixed array_sum(array input);
Sum all array number

25. Array_unique() function


Array array_unique(array array);
Remove all repeat values

26. Array_unshift() function


Int array_unshift(array array, mixed var [, mixed …]);
將數個元素加入到 array 開頭

27. Array_values() function


Array array_values(array input);
以 array 傳回 input array 的所有值
新 array 以 numeric key 做存取
28. Array_search() function
Mixed array_search(mixed needle, array haystack [, bool strict]);
在 haystack array 裡 search neele 的值, 如 strict 指定為 true, 則需要符合型態才算,
如 search 到, 則回傳 value 的 key

29. Count() function


Int count(mixed var);
Return var 元素的總數

30. Current() function


Mixed current(array array);
Return array value of current position pointer

31. Each() function


Array each(array array);
Return 4 key array of current position pointer and direct to next position
4 key array:
0/key 1/value

32. End() function


End(array array);
Direct pointer to last position

33. In_array() function


Bool in_array(mixed needle, array haystack [, bool strict]);
Search array, if haystack exists needle string, it return true
If Strict is true, it should by same data type and exists needle string, than return true

34. Key() function


Mixed key(array array);
Return index of associative array key pointer

35. Ksort() function


Int ksort(array array);
依 array key 做 sort 動作 a-z
36. List() function
Void list(…);
設定多個 var
<table>
<tr>
<th>Employee name</th>
<th>Salary</th>
</tr>
<?php
$result = mysql($conn, “SELECT id, name, salary FROM employees”);
While(list($id, $name, $salary) = mysql_fetch_row($result)) {
Print(“ <tr>\n”.
“ <td><a href=\”info.php3?id=$id\”>$name</a></td>\n.
“ </tr>\n”);
}
?>
</table>

37. Next() function


Mixed next(array array)
傳回下一個 pointer value
Notes: 如下一個 pointer value 是 0 或””或 array 尾則回傳 false
可利用 each() function 檢查

38. Pos() function


Mixed pos(array array);
Pos() function same as current() function

39. Prev() function


Mixed prev(array array);
將 array pointer 往前移一位後,再回傳 value

40. Reset() function


Mixed reset(array array);
Reset pointer position to first position and return first array value

41. Rsort() function


Void rsort(array array); //array value will sort by z->a
42. Sizeof() function
Int sizeof(array array);
Return total array key number

43. Sort() function


Void sort(array array); //array value sort by a->z
Notes: it will not change the key or index

44. Uasort() function


Void uasort(array array, function cmp_function);
……………

45. Uksort() function


Void uksort(array array, function cmp_function);
……………

46. Usort() function


Void usort(array array, function cmp_function);
…………….

You might also like