PHP study notes – array articles (10)

  • 的头像-Tumi
  • 39 day agopublish

array_unique (array);

effects:

removing duplicate array values, a first value if the key key element of different retention

Parameters:

array: array

return value:

returns an array of de-duplicated

examples: removing the same array value

  $ a = array ( 'a' => 'red', 'b' => 'blue', 'c' => 'red'); $ res = array_unique ($ a); print_r ($ res ); output: Array ([a] => red [b] => blue)  


array_unshift (array, value1, value2, value3 ………)

Function:

is inserted into one or more new elements

at the beginning of the array parameters:

array : need to insert a new element of the array

VALUE1: new element

value2: new element

return value:

after returning inserting a new element of the array a number

example: a plurality of insert elements into the array

  $ a = array (0 => 'red', 'a' => 'red', ' b '=>' blue '); $ res = array_unshift ($ a,' name ',' age ',' purple ',' yellow '); print_r ($ res); // insertNumber of elements number print_r ("\ n"); Print_R ($ a); // Insert an array output result: 7 array ([0] => Name [1] => AGE [2] => Purple [3] => yellow [4] => red [A] => red [b] => blue)  

Array_VALUES (Array

Returns all values ​​in the array

parameter:

Array: array

Return value:

Returns all values ​​in array

: Get all values ​​in array

$ a = array (0 => ‘red’, ‘a’ => ‘Red’, ‘b’ => ‘blue’); $ res = array_values ​​($ a); print_r ($ r); output: array ([0] => red [1] => red [2 ] => Blue)

 Array_walk (array, strng, parames);  : 
use Custom Functions Do Key and Value in array


Parameters:

Array: array

String: Custom Function Name

Parames: Optional, custom parameter

return value:

Successfully returned to true, failed to return false

: Modifying the value of the group to a value of Purple

$ a = array (‘a’ => ‘Yellow’, ‘B’ => ‘Hello’, ‘C’ => ‘Red’); Function Myfunction (& $ VALUE, $ KEY, $ STR) {IF (‘a’ === $ key) {$ Value = ‘purple’; Echo $ Str. “\ n”;}} Array_walk ($ A, ‘MyFunction’, ‘Modification Success’); Print_R ($ A); Output: Modify Success Array ([A] => Purple [B] => Hello [C] => RED)


Array_walk_recursive (array, string, parames);

Role:
 Same as Array_walk, Different: Array_walk _Recursive () can operate an array in an array   parameter: 
Array: array


String : Custom Function Name

Parames: Optional, custom parameter

Return value:

Successfully returned True, failed to return false

Example: Modify the value in the array

$ b = array (‘d’ => ‘Black’, ‘e’=> ‘Blue’); $ a = array (‘a’ => ‘Yellow’, ‘b’ => ‘Hello’, ‘c’ => ‘red’, $ b); Function MyFunction (& $ VALUE, $ KEY, $ STR) {IF (‘a’ === $ key || ‘d’ === $ key) {$ value = ‘purple’; Echo $ Str. “\ n”;}} array_walk ($ A, ‘myfunction’, ‘modification success’); Print_R (“use array_walk \ n”); Print_r ($ A); Print_r (“\ n”); array_walk_recursive ($ A, ‘MyFunction’, ‘Success’;); Print_R (“uses array_walk_recurs \ n”); Print_r ($ A); output results: Modify Successfully using array_walk // only modify the value of Key ARRAY ([A] => purple [b] => Hello [C] => Red [0] => Array ([D] => Black [e] => blue)) SuccessSuccess uses array_walk_recurs // key to modify success array ([A] => purple [b] => Hello [c] => red [0] => Array ([D] => PURPLE [E] => blue))

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment