PHP Common Array function Introduction, PHP Array function _php Tutorial

Source: Internet
Author: User

PHP Common Array function Introduction, PHP array function


In the programming of the manual is not necessary, so you will learn to use the existing things, such as PHP in the array processing function has a sorting function, why still writing is to pay the strength to write bubble or heap or fast row.

Programming is an indirect process, but also the process of reuse, to write good code is not necessary to do the design pattern to do support, may be for beginners to learn the design mode is a bit difficult (as I looked at the design mode, it is a bit laborious), but when you have a certain amount of code accumulation, in the study of design patterns, Feeling the design pattern is really useful and can help you write beautiful code. Talk about a bit of a deviation, or to summarize the PHP operation of the common function of the array.

The following summarizes the array of commonly used functions, perhaps some readers will feel a little bit, crowds, if you feel there are other commonly used array processing functions, give a comment to leave Bai, do not skimp on their knowledge, and others to share things is not a very happy thing. Also, the following code from my hand, but two years ago written code, you are welcome to criticize.

Array_splice () deletes the specified element in the array

Array_splice (array name, the number of the previous delete, the size of the new array); There is no third parameter, there is no return array, no third argument, the meaning of the second argument is to keep a few in the past

Exp:

<?php  $my _array=array (  //create array    "hehe" = "haha",    "A" and "Lu",    "Lu" and "GE"  );  $new =array_splice ($my _array,1,3);  Use Array_splice (the array name, the number of previous deletions, and the size of the new array);  Var_dump ($new);? >

Result: Array (2) {["A"]=> string (2) "Lu" ["Lu"]=> string (2) "GE"}

2, the traversal of the foreach () array

Usage: foreach (array as key name = = key value) or foreach (array as key value)

Exp:

<?php  $my _array=array (  //create array    "hehe" = "haha",    "A" and "Lu",    "Lu" and "GE"  );  foreach ($my _array as $key = + $value)  {    echo $key. = ". $value."
"; }? >

Output Result:

Hehe=>hahaa=>lulu=>ge

3. Sorting of arrays

(1) sort () and rsort () key values sorts () from small to large, rsort () from large to small

Sort () Exp:

<?php  $my _array=array (1,2,3,6,7,8,9,4,5);//create array  sort ($my _array);  foreach ($my _array as $keys = + $value)  {    echo $keys. = ". $value."
"; }? >

Output Result:

  0=>1  1=>2  2=>3  3=>4  4=>5  5=>6  6=>7  7=>8  8=> 9

Rsort () Exp:

<?php  $my _array=array (1,2,3,6,7,8,9,4,5);//build Array  rsort ($my _array);  foreach ($my _array as $keys = + $value)  {    echo $keys. = ". $value."
"; }? >

Output Result:

  0=>9  1=>8  2=>7  3=>6    4=>5  5=>4  6=>3  7=>2  8=>1

(2). Asort () and Arsort () are the same as above, but do not change the corresponding relationship between the key name and the key value.

Exp:

<?php  $my _array=array (1,2,3,6,7,8,9,4,5);//build Array  asort ($my _array);  foreach ($my _array as $keys = + $value)  {    echo $keys. = ". $value."
"; }? >

Output Result:

0=>11=>22=>37=>48=>53=>64=>75=>86=>9

(3) Ksort () and Krsort () are the size of the key names sorted

4. Mathematical class functions for arrays

Array_sum () calculates the number of all key values and count () elements of an array

Exp:

<?php  $my _array=array (1,2,3,6,7,8,9,4,5);//create Array  echo array_sum ($my _array);? >

Output results: 45

5. Other functions

Array_unique () Removes the same element from the array

In_array () detects if a value is in the array (returns True and false)

Array_search () returns a key or value that returns the key name of the key value

Shuffle () disrupts the original array

<?php  $my _array=array (1,2,3,6,7,8,9,4,5,5,5,5);//Create an array  array_unique ($my _array);//Remove the same element from the array  var_ Dump ($my _array);  echo "
"; Echo In_array (5, $my _array); echo "
"; $new =array_search (6, $my _array);//returns the key value corresponding to the key name Echo $new; >

Output Result:

Array (+) {[0]=> int (1) [1]=> int (2) [2]=> int (3) [3]=> int (6) [4]=> Int (7) [5]=> Int (8) [6]=> Int (9 ) [7]=> Int (4) [8]=> int (5) [9]=> int (5) [10]=> int (5) [11]=> int (5)} 13


How many of the most commonly used functions in an array of PHP arrays well, people give them a list.

foreach (array_expression as $key = $value)
The key () function is used to read the index value of the data currently pointed to by the pointer current () is to obtain the current position of the pointer content data
The count () function returns the length value of the destination array
Control functions for array pointers
Pnext (), prev (), End (), and Reset ()
Next () is responsible for moving the pointer backwards, prev () is responsible for moving the pointer forward; end () points the pointer to the last element in the array, and the reset () function moves the current pointer unconditionally to the first index position.
In_array (mixed $needle, array $haystack [, BOOL $strict]); Determines whether an array element exists or not.
Array_search (), the function returns the corresponding key name. The array_key_exists () function can retrieve whether the given key name (index) exists in the array.

And if you need me, I can give you PPT.

PHP Common functions

In fact, there is no need to go around the so-called "common functions", so-called commonly used is different, someone often use a function, but does not mean that you will always use. PHP function A lot, standing PHP help manual is the key, encountered unfamiliar on the check, check the number of times, naturally become "common".

In general, the functions of strings, arrays, and database classes are relative to the use of more than one category.

Here is a list of functions, for reference only.

============================================

=============================== Time Date ===============================
Y returns the last two digits of the year, the Y-year four digits, the M-month number, and the M-month English. D Month Date number, D week a few English
$date =date ("y-m-d");

Include,include_once.require,require_once
Require ("file.php") will be read into the file specified by require before the PHP program executes, if the error is fatal.
The Include ("file.php") can be placed anywhere in the PHP program, and the PHP program will read into the include specified file when it is executed, such as an error prompting

=============================== Output Print ===============================
sprintf ("%d", "3.2");//format only, return formatted string, not output.
printf ("%d", "3.2");//format and output
Print ("3.2");//Output only
echo "Nihao", "AA";//can output multiple strings
Print_r (Array ("A", "B", "C"));//The array's key values and elements are displayed sequentially

=============================== commonly used String functions ===============================

Gets the string length, the number of characters, the space is also counted
$STR = "Sdaf SD";
$len =strlen ($STR);

Use the string in the first argument to concatenate each element in the following array and return a string.
$str =implode ("-", Array ("A", "B", "C"));

String segmentation method, return an array, with the characters in the first argument to split the following string, the specified characters are truncated before and after the character, if the specified character at the beginning or end of the returned array of the beginning or end of the element is an empty string
A null value is returned to the corresponding element of the array without splitting it into a string. The last limit returns the length of the array, which is not limited, and is always split down.
$array =explode ("A", "ASDD ... Remaining full text >>

http://www.bkjia.com/PHPjc/851337.html www.bkjia.com true http://www.bkjia.com/PHPjc/851337.html techarticle php commonly used array functions are introduced, PHP array functions in the programming of the manual is not necessary, so you will learn to use the existing things, such as the PHP array processing function has a sorting function ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.