Home >Backend Development >PHP Tutorial >Summary of array functions in PHP5_PHP tutorial

Summary of array functions in PHP5_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:38:20875browse

I don’t know how PHPbeginners learn the function, but I think learning it must be purposeful and purpose-driven. Take array functions as an example! How do we learn so many array functions? In fact, we just need to be clear about what operations we need to do on the array, and then classify these operations, and have a rough impression in our mind. Then with your practice, you will know these functions very clearly. What I remind you here is Keep the manual handy! Without further ado, array functions generally fall into the following categories (don’t be afraid if you see English):
Outputting arrays
Creating arrays
Testing for an array
Locating array elements
Traversing arrays
Determining array size and element uniqueness
Sorting arrays
Merging, slicing, splicing, and dissecting arrays

Outputting arrays
print_r()
For those who are not familiar with this, I don’t want to say more! The most basic function, of course, needs to output an array or use a loop structure to output, but why not use such a convenient output function?

Creating arrays
array()
I don’t want to talk about this function anymore. In fact, it can only be regarded as a language structure. Everyone has used it when creating a new array. For those who have never used it, I can only say, brother, I am speechless!
list()
This function is the same as the array() mentioned above. It is just a language structure. It assigns values ​​to a set of variables in one step!
Please check the manual for specific examples!
range();
array range ( mixed low, mixed high[, number step] )
Create an array containing cells in the specified range
Example:$arr =range(0, 6);
is equivalent to $arr =array(0, 1, 2, 3, 4, 5, 6);
General data is arranged from low to high. If low> high, it is arranged from high to low;

Testing for an array
is_array();
Test whether the variable is of array type. If it is, it will return true, if it is not, it will return false. It is a very basic and practical function!

Adding and removing array elements
array_push()
int array_push ( array &target_array,mixed var [, mixed ...] )
Add one or more cells to the array from the end! Returns the total number of cells in the new array!
Example:

Copy PHP content to clipboard
PHP code:
<font face="新宋体">$states <span style="color: rgb(0,119,0)">=array(<code style="font-size: 12px"><font face="新宋体">$states <span style="color: rgb(0,119,0)">=array(</span><span style="color: rgb(0,0,187)">‘Ohio’</span><span style="color: rgb(0,119,0)">, </span><span style="color: rgb(0,0,187)">‘New York’</span></font><font face="新宋体"><span style="color: rgb(0,119,0)">);<br> </span><span style="color: rgb(0,0,187)">array_push</span><span style="color: rgb(0,119,0)">(</span><span style="color: rgb(0,0,187)">$states</span><span style="color: rgb(0,119,0)">, </span><span style="color: rgb(0,0,187)">‘California’</span><span style="color: rgb(0,119,0)">,</span><span style="color: rgb(0,0,187)">‘Texas’</span></font><font face="新宋体"><span style="color: rgb(0,119,0)">);<br> </span><span style="color: rgb(255,128,0)">//output: array((‘Ohio’,‘New York’ , ‘California’,‘Texas’);</span><br> <br> </font>'Ohio'
, ' New York'); array_push($states, 'California','Texas'); //output: array(('Ohio','New York' , 'California','Texas');

array_pop();
Pops and returns the last element of the array and decrements the array length by one. The array pointer will be reset after use!
Example:

Copy PHP content to clipboard
PHP code:
<font face="新宋体">$states <span style="color: rgb(0,119,0)">=array(<code style="font-size: 12px"><font face="新宋体">$states <span style="color: rgb(0,119,0)">=array(</span><span style="color: rgb(0,0,187)">‘Ohio’</span><span style="color: rgb(0,119,0)">, </span><span style="color: rgb(0,0,187)">‘New York’</span></font><font face="新宋体"><span style="color: rgb(0,119,0)">);<br> </span><span style="color: rgb(0,0,187)">$state </span><span style="color: rgb(0,119,0)">= </span><span style="color: rgb(0,0,187)">array_pop</span><span style="color: rgb(0,119,0)">(</span><span style="color: rgb(0,0,187)">$states</span></font><font face="新宋体"><span style="color: rgb(0,119,0)">);<br> </span><span style="color: rgb(255,128,0)">//output: New York</span><br> <br> </font>'Ohio'
,

' New York'); $state
= array_pop(
$states
); //output: New York


array_shift();
Similar to array_pop, except that it removes and returns the first value of the array. Numeric key names will start counting from zero again, while text key names will remain unchanged!


array_unshift();
Similar to array_push, except that it inserts data from the head of the array! Numeric key names will start counting from zero again, while text key names will remain unchanged!
array_pad();
array
array_pad
( array input, intpad_size, mixed pad_value ) Fill the array to the specified length with values! If pad_size is positive, padding starts from the right side; if pad_size is negative, padding starts from the left side; if it is less than the length of the array, the array remains unchanged! See the manual for examples!
Locating array elements
in_array();

Check whether a certain value exists in the array. I won’t go into the basics. Please refer to the manual for details!
array_keys();
array array_keys
( array input [,mixed search_value [, bool strict]] )
Returns all key names in the array. If search_value is selected, the selected key name is returned! Since PHP 5], you can use the strict parameter to perform congruent comparisons (===).

array_key_exists();
bool
array_key_exists ( mixed key,array search ) Checks whether the given key name or index exists in the array, and returns true if it exists. Remember to check the manual for some more specific applications!

array_values(); array array_values ( array input ) Similar to array_keys()! What is returned is all the key values ​​​​of the array!

array_search();
mixed array_search
( mixed needle,array haystack [, bool strict] )

Search for the needle parameter in haystack and return the key name if found, otherwise return

. If needle is a string, the comparison is case-sensitive! If strict is true, types must be compared
If needle appears more than once in haystack, return the first matching key. To return all keys that match a value, array_keys() should be used instead with the optional parameter search_value!
http://www.bkjia.com/PHPjc/486501.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486501.htmlTechArticleI don’t know how PHP beginners learn functions, but I think learning them must be purposeful. It is purpose-driven, take array functions as an example! With so many array functions we...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn