PHP Learning Path (vi)

Source: Internet
Author: User
Tags array definition

2017.08.23-24

Day 6 Wednesday-Thursday Sunny (typhoon)

HP Array Definition

An array is a language structure consisting of a key-value pair, similar to the hotel's room number, which is similar to what is stored in a hotel room.

If you go to a hotel, the waiter will tell you how much room number, the specific room has stored what, it will need to enter the room according to the room number to know.

The figure below is a clearer explanation of what an array is.

Do you really want to create your own array after you know the array?

$arr = array();Represents creating an empty array and assigning the created empty array to the variable $arr.

Indexed array initialization of PHP arrays

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

Indexes and associations two words are for the keys of an array.

The index array is first introduced, the index array is an array of integers, and the key's integer order starts at 0, and so on.

Here's a diagram to illustrate:

Can be implemented using the following code:

$fruit = array("苹果","香蕉","菠萝");Note that the keys of the array start from 0. You can use print_r($fruit); statements to output array keys and their corresponding values.

Indexed array Assignment of PHP arrays

There are three ways to assign an indexed array:

The first is to assign a value with the name of the array variable followed by a square bracket, and of course the key in the index array, in parentheses, must be an integer. Like what$arr[0]=‘苹果‘;

The second is to array() create an empty array, use => symbols to separate the keys and values, the left side represents the key, and the right side represents the value. Of course, in an indexed array, the key must be an integer. Like whatarray(‘0‘=>‘苹果‘);

The third type: Use to array() create an empty array, directly in the array in English single quotation marks or English double quotation marks " , the array will default to establish a 0-based integer key. For example, array(‘苹果‘); this array is equivalentarray(‘0‘=>‘苹果‘);

Access index array contents of PHP array

Sometimes we need to take the contents of the array as values out of the What to do? The values in the array are accessed by using the key in parentheses, followed by the name of the array variable.

For example:

$fruit = Array (' Apple ', ' banana '), $fruit 0 = $fruit [' 0 '];p rint_r ($fruit 0);//result for Apple

The function of the above code: first take an array $fruit first value and then assign to a variable $fruit0, the last output variable value.

The Foreach loop of the PHP array accesses the values in the indexed array

The Foreach loop can access all the values in the array, and we'll use a Foreach loop to iterate through the values in the indexed array.

For example:

$fruit =array (' Apple ', ' banana ', ' pineapple '), foreach ($fruit as $k = + $v) {    echo ' <br> '. $k. ' Value is: '. $v;}

If you don't remember you can review the Foreach function (typically used to traverse the function Hee hee)

Initialization of an associative array of PHP arrays

Looking back at what was described earlier, PHP has two arrays: an indexed array, an associative array.

Indexes and associations two words are for the keys of an array.

First, the associative array is an array of exponential groups whose keys are strings.

Can be implemented using the following code:

$fruit = Array (    ' apple ' = ' apple ',    ' banana ' and ' banana ',    

You can use print_r($fruit); statements to output array keys and their corresponding values.

Type:

<?php
Creates an associative array with the key "orange" for the associative array, and the value is "orange"
$fruit = Array (
' Orange ' = ' orange ',
' Didi ' = ' BBB ',
' CiCi ' = ' CCC '
);
Print_r ($fruit);
?>

Output:

Array
(
[Orange] = orange
[Didi] = BBB
[CiCi] = CCC
)

An associative array assignment for an array of PHP

There are two ways to assign an associative array:

The first is to assign a value with the name of an 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‘]=‘苹果‘;

The second is to array() create an empty array, use => symbols to separate the keys and values, the left side represents the key, and the right side represents the value. Of course, the key must be a string in the associative array. Like whatarray(‘apple‘=>‘苹果‘);

Access associative array contents of PHP array

Use the name of the array variable followed by the brackets + key to access the values in the array, with the keys enclosed in single or double quotation marks.

Like what:

$fruit = Array (' apple ' = ' apple ', ' banana ' = ' banana ', ' pineapple ' = ' pineapple '); $fruit 0 = $fruit [' banana '];p Rint_r ($fruit 0);

The function of the above code: first the value of an array $fruit key to the banana string is taken out and then assigned to a second variable $fruit0, the last output variable value.

The Foreach loop of the PHP array accesses the values in the associative array

The Foreach loop can access all the values in the array, and we'll use a Foreach loop to iterate through the values in the associative array.

For example:

$fruit =array (' apple ' = ' apple ', ' banana ' + ' banana ', ' pineapple ' and ' pineapple '), foreach ($fruit as $k + $v) {    echo ' <br> Fruit's English Key name: '. $k. ', the corresponding value is: '. $v;}

PHP Classes and objects

Class is the basic concept of object-oriented programming, popular understanding of the class is the reality of a certain kind of things abstract, such as cars can be abstracted as a class, the car has a name, tires, speed, weight and other properties, can have shift, forward, backward and other operating methods.

A typical method for defining a car class is:

Class Car {    $name = ' car ';    function GetName () {        return $this->name;    }}

A class is a structural description of something, and an object is a concrete example of something, such as a car, which can be understood as the general class of a car, but the car is a specific car object.

object is instantiated with the New keyword:

$car = new car (); Echo $car->getname ();

Classes are similar to objects, but there are essentially differences, classes are abstract concepts, and objects are concrete instances. Class can make a program reusable.

This is the weight of today ~

2017/08/24

23:00

PHP Learning Path (vi)

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.