Getting Started with PHP (6) PHP arrays

Source: Internet
Author: User

Data is a collection of sets of data. Each element of an array consists of two parts, a key and a value.


The array name in PHP must start with $ the first character must be a letter or an underscore and cannot begin with a number.

For example

$array 1 = Array ("Joedlut" =>12, "Samshit" =>14, "Gotohell" =>15);

Note that in the same program, scalar variables and array variables cannot have duplicate names.

Array names are case sensitive


Methods for creating Arrays in PHP:

Through the array () function

Array array (mixed ...)

$array 1 = Array ("java" = "java", "php" = "php", "C + +" + "C + +");p rint $array 1["java"];//to print an element of an array by indexing Print_r ($array 1);


Types of arrays in PHP:

numeric indexed arrays (using arrays as keys) and associative arrays (using strings as keys)


Output array: Note that the structure of the entire array is output (feeling useless)

Use Print_r ($array _name) or Var_dump ($array _name)

Print_r ($array 1); Var_dump ($array 1);

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/87/C0/wKioL1fg7--wQrDoAAAnv69th4g494.jpg-wh_500x0-wm_3 -wmp_4-s_2316447302.jpg "title=" 1.jpg "alt=" Wkiol1fg7--wqrdoaaanv69th4g494.jpg-wh_50 "/>

How do I iterate through an array?

Using the Foreach

foreach ($array 1 as $key = + $value) {print "$key-------$value". " <br> ";}


How to get the number of array elements

int count (mixed array)

echo Count ($array 1); 3


Merging arrays

Array Array_merge (array Array1,array array2 ...) If the string key name of the two array is the same, the latter overrides the previous value. For values with the same number key name will not overwrite

Array array_merge_recursive (Array1,array2,array3 ...) Unlike the above, the value of two elements with the same string key name will not be overwritten. Elements with the same string key name make up a new array to store as elements

$array 1 = Array ("Java" + "Java Combat development", "php" = "PHP from Beginner to Proficient"); $array 2 = Array ("java" = "Java from getting started to mastering", "C + +" = "c+ + From Beginner to Proficient "); $array 3 = Array_merge ($array 1, $array 2); $array 4 = array_merge_recursive ($array 1, $array 2);p Rint_r ($array 3) ;p Rint_r ($array 4);

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/87/C1/wKioL1fg9mew2TIgAAA-rcjoaFg025.jpg-wh_500x0-wm_3 -wmp_4-s_1428644178.jpg "title=" 2.jpg "alt=" Wkiol1fg9mew2tigaaa-rcjoafg025.jpg-wh_50 "/>


Convert a string to an array

Array explode (string separator,string string);

$str = "Joe,sam,shit"; $array = Explode (",", str);//print_r ($array);


Convert an array to a string

$array = Array ("Joe", "Sam", "Shit"), $str = Implode ("", $array);p rint $str;


To sort a numeric indexed array

BOOL Sort (array &array,int sort_flags)//Ascending

BOOL Sort (array &array,int sort_flags)//Descending

Types of Sort_flags

Sort_numeric to sort as numeric values

Sort_regular is sorted in a normal way (does not change its type) by default

Sort_string sorting as a string

$array = Array (90,23,345,12,-12,34), sort ($array),//incoming reference, the value of the original array is changed Print_r ($array);


To sort an associative array

int Ksort (array array,int sort_flags);//Sort the index values of the associative array

int Asort (array array,int sort_flags);//Sort an array of element values ascending

int Arsort (array array,int sort_flags);//Descending

$array 2 = Array ("Java" =>12, "PHP" =>8, "C + +" = "+"), Ksort ($array 2);p Rint_r ($array 2); echo "<br>" Asort ($array 2);p Rint_r ($array 2), echo "<br>", Arsort ($array 2);p Rint_r ($array 2);

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/87/C5/wKiom1fg-7HTr9enAAArR6njvPA813.jpg-wh_500x0-wm_3 -wmp_4-s_3719680145.jpg "title=" 3.jpg "alt=" Wkiom1fg-7htr9enaaarr6njvpa813.jpg-wh_50 "/>



This article is from the "thick Product Thin Hair" blog, please make sure to keep this source http://joedlut.blog.51cto.com/6570198/1854593

Getting Started with PHP (6) PHP arrays

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.