Php Array Function

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Php Array Function as PDF for free.

More details

  • Words: 1,273
  • Pages: 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”; array(“a”=>”orange”, “b”=>”banana”, “c”=>”apple”); “numbers” => array(1,2,3,4,5,6); “holes” => array(“first”, 5 => “second”, “third”); ); ?> Same

2. Array_walk() function Int array_walk(array arr, string func); Example ”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 ”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); Output: Array( [1] => 1 [2] => 2 [3] => 1 [Hello] => 2 [World] => 1) Array_diff() function Array array_diff(array array1, array array2, array...); Output Array ( [1] => white ) 6. Array_filter() function Array array_filter(array input [, mixed callback]); Example <pre>
$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); ?> 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> ”a”, “B”=>”b”, “C”=>”c”, “D”=>”c”); $arr_flip = array_flip($arr); Print_r($arr); Print_r($arr_flip); ?> 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 …]);
$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 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> Array ( [0] => 9 [1] => 1 [2] => 3 [3] => 0 [4] => 0 ) Array ( [0] => 5

[1] [2] [3] [4] [5] [6] [7] [8] [9]

=> => => => => => => => =>

5 5 5 5 5 5 9 1 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

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 \n”. “ \n. “ \n”); } ?>
Employee name Salary
$name
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); …………….

Related Documents

Php Array Function
July 2020 1
Php Mysql Function
July 2020 4
Php Image Function
July 2020 1
Array
June 2020 19