PHP Basics-Arrays

Source: Internet
Author: User
Tags php basics

PHP arrays

PHP has two types of arrays: indexed arrays, associative arrays.

    • Indexed array

  Brief introduction

An indexed array is an array of exponential groups whose keys are integers, and the integer order of the keys starts at 0, and so on. As shown in the following code:

$fruit=array("Apple", "banana", "pineapple");

  Assign value

    The
    1. assigns a value with the name of an array variable followed by a square bracket, and of course, the key in the index array, within the brackets, must be an integer. For example, $arr [0]= ' Apple ';
    2. use array () to create an empty array, using => symbols to separate keys and values, the left for the key, and the right to represent values. Of course, in an indexed array, the key must be an integer. For example, array (' 0 ' = ' apple ');
    3. use array () to create an empty array, directly in the array in English single quotation marks or double quotation marks in English ", the array defaults to the key of the integer starting from 0. such as array (' Apple '); This array is equivalent to array (' 0 ' = ' apple ');
<? PHP     $arr=array();//Create an empty array     for ($i=0;  $i<10; $i+ +) {//loop traversal, assigning a value        to the array $arr[$i]=$i+1;    }     Print_r ($arr);? >

  Accessing the value of an array

<? PHP     // from the array variable $arr, read the value    of the key 0 $arr Array (' apples ', ' bananas ');     Print_r ($arr[' 0 ']);? >
    1. For iterating through the values in the array
<? PHP     Header ("content-type:text/html; Charset=utf-8 ");     $fruit=array(' Apple ', ' banana ', ' pineapple ');      for ($i= 0; $i<count($fruit); $i+ +)        {echo ' <br> array '.  $i. ' The value is: '. $fruit [$i];    }? >

2.foreach iterating through the values in the array

<? PHP     Header ("content-type:text/html; Charset=utf-8 ");     $fruit=array(' Apple ', ' banana ', ' pineapple ');     foreach ($fruitas$key=$value) {        echo ' <br > '. $key. ' The value is: '. $value ;    }? >
    • Associative arrays

An associative array is an array of exponential groups whose keys are strings.

<? PHP     Header ("content-type:text/html; Charset=utf-8 ");     $fruit=arr (        ' apple ' = ' apple ',        ' banana ' + ' banana ',        ' pineapple ' = ' pineapple     ') );? >

Assign value

    1. Assign a value with the name of the array variable followed by a square bracket, of course, the key in the associative array, in parentheses, must be a string. Like what$arr[‘apple‘]=‘苹果‘;
    2. array()creates an empty array, uses => symbols to separate keys and values, the left represents the key, and the right represents the value. Of course, the key must be a string in the associative array. Like whatarray(‘apple‘=>‘苹果‘);

  Accessing the value of an associative array

<? PHP     // from the array variable $arr, read the value    of the key to Apple $arr Array (' apple ' = ' apple ', ' banana ' and ' banana ', ' pineapple ' and ' pineapple ');     Print_r ($arr[' Apple ']); >

A Foreach loop accesses the value of an associative array

<? PHP     Header ("content-type:text/html; Charset=utf-8 ");     $fruit=array(' apple ' = ' apple ', ' banana ' = ' banana ', ' pineapple ' = ' pineapple ');     foreach ($fruitas$key=$value) {        echo ' <br > '. $key. ' The corresponding Chinese is '. $value ;    }? >

PHP Basics-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.