PHP Array 1

Source: Internet
Author: User


1. Basic form definition Array $array =array ();p Rint_r can print complex data type function 2. Define Array $array1 = Array (' John Doe ', ' ning ', ' Ling '); 3. Add a function to the array (an array of elements that need to be added, element 1, Element 2): Array_push ($array 1, ' Two dog ', ' dog Egg '); 4. Range (start, end, interval) returns an array of values $ARRAY3 = range (1,10,2); Echo ' <pre> '; Output Print_r ($array 3); Echo ' </pre> ';//array ([0] = 1 [1] = 3 [2] = 5 [3] = 7 [4] = 9) 5. Key-value pairs form $ar Ray5 = Array (); $array 5[' zhang '] = ' zhangsan '; $array 5[' wang '] = ' Wangbadan '; $array 5[' li '] = ' lisi '; $array 5[' tang '] = ' Tangdasha '; Echo ' <pre> ';p rint_r ($array 5); Echo ' </pre> ';//array ([Zhang] = zhangsan [Wang] = Wangbadan [Li] = lisi [tang] = Tangdasha) 6. Iterate over the array Foreachforeach ($array 5 as $key + $value) {echo $key. ‘ : ‘. $value. ' <br> ';} zhang:zhangsanwang:wangbadanli:lisitang:tangdasha  Note: The name of the key is either the same, or the back will cover the previous 7. Array Length: Count ($array 6); 8. Delete the elements in the array $array7 = array (1,2,3,4); Array_splice ($array 7,1,1); Array_splice (array, index,length);  9. Two-d array $array9 = Array (' Autumn Dress ', ' Autumn Pants '), array (' cotton-padded ', ' trousers ', ' shoes '); Echo ' <pre > ';p rint_r ($ARRAY9); Echo ' </pre> ';//array ([0] = = Array ([0] = [1] = Autumn pants) [1] = = Array ([0] = coat [1] =&gt ; Trousers [2] = shoes) 10. Key-value pairs form a two-dimensional array $array8 = array (' Autumn ' =>array (' Autumn clothes ', ' Autumn Pants '), ' Cotton ' =>array (' padded ', ' trousers ', ' trousers ')); Echo ' <pre > ';p rint_r ($array 8); Echo ' </pre> ';//array ([autumn] = = Array ([0] = Autumn clothes [1] = Autumn pants) [cotton] = Array ([0] =& Gt Coat [1] = trousers [2] = trousers) 11. Traverse the two-dimensional array traversal Array9$array9 = Array (' Autumn clothes ', ' Autumn Pants '), array (' padded ', ' trousers ', ' shoes ')); for ($i = 0; $i < Count ($array 9), $i + +) {for ($k = 0; $k < count ($array 9[$i]), $k + +) {echo $array 9[$i] [$k]. ' ---‘;} Echo ' <br> ';} Autumn Clothes---autumn trousers---cotton---trousers---shoes---      

PHP Array 1

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.