PHP array definition Getting Started Tutorial detailed _php tutorial

Source: Internet
Author: User
Tags array definition
There are several ways to define arrays in PHP, such as Array (), or arr[] to implement arrays, let me give you a detailed description of the various techniques for PHP array definitions.

PHP array is an important concept, it contains a lot of functions, convenient for people to develop ... The array is now sorted to make it easy to query and

Use.
First, the definition of the PHP array ... The PHP array contains two items, key and value, which can be used to obtain the corresponding value, where key can be

To be numeric and associative, such as $array[0], $array [one] ...
Create an array
The array declaration in PHP is a little bit different from other languages, but it can be declared as one-dimensional, two-dimensional, three-dimensional, multidimensional, etc.
$array [0] = 1, $array = Array (n/a); A one-dimensional array that contains only three values, which are numeric arrays that can be referenced with $array

[0] to represent 1, you can omit the index when creating a numeric array

Creating arrays in PHP is defined using the array () structure, for example:

The code is as follows Copy Code


$people =array (' name ', ' Sex ', ' nation ', ' brith ');

And how to display the values of each element in the array, we use an index starting at 0, and the index number is in square brackets after the variable name, such as

The code is as follows Copy Code


$people =array (' name ', ' Sex ', ' nation ', ' birth ');
Echo $people [2];
?>

The output of $people[2] is displayed as nation (the first item in the index is counted from 0).

In addition to supporting numeric index arrays, PHP supports related arrays. The so-called correlation array, that is, can be customized to replace the keyword is not intuitive

Numeric indexes, such as:

The code is as follows Copy Code


$peoples =array (' xm ' = ' name ', ' xb ' = ' sex ', ' mz ' = ' nation ', ' cs ' = ' birth ');
echo $peoples [' CS '];
?>

Using a correlation array makes the selection of the output intuitive (no need to pre-compute the index number and then output), and the defined keyword and value are used between

the "= =" Symbol definition.

Depending on the way the PHP array elements are displayed, you can also create numbers directly, without having to declare and initialize the array () as a variable.

Like what

The code is as follows Copy Code


$people [0]= ' name ';
$people [1]= ' sex ';
$people [2]= ' Nation ';
$people [3]= ' Brith ';

Or

The code is as follows Copy Code


$peoples [' XM ']= ' name ';
$peoples [' XB ']= ' sex ';
$peoples [' Mz ']= ' nation ';
$peoples [' cs ']= ' birth ';

The size of the array varies dynamically depending on how many elements are added.

The code is as follows Copy Code


Indexed array
$user [0]=1;//User number
$user [1]= "Zhangsan";//username
$user [2]=10;//Age
$user [3]= "Nan";//gender
Echo '



Echo '
';

Associative arrays
$user ["id"]=1;
$user ["Name"]= "Zhangsan";
$user ["Age"]=10;
$user ["Sex"];
$user ["Age"]=90;//assignment
echo $user ["name"];//output

Declaring an array using array ()
$user =array (1, "Zhangsan", Ten, "Nan");
Declaring an associative array using array ()
$user =array ("id" =>1, "name" = "Zhangsan", "Age" =>10, "sex" = "Nan");

declaring multidimensional Arrays (multiple records) to hold multiple user information records in a table
$user =array (
Call this line with $user[0], such as calling the name in the record, $user [0][1]
Array (1, "Zhangsan", Ten, "Nan"),
Call this line with $user[1], such as calling the name in the record, $user [1][1]
Array (2, "Lisi", "NV")
);

Arrays save multiple tables with multiple records per table
$info =array (
"User" =>array (
Array (1, "Zhangsan", Ten, "Nan"),
Array (2, "Lisi", "NV")
),

"Score" =>array (
Array (1,90,80,70),
Array (2,60,40,70)
)



);

echo $info ["score"][1][1];//output 60,

http://www.bkjia.com/PHPjc/628676.html www.bkjia.com true http://www.bkjia.com/PHPjc/628676.html techarticle There are several ways to define arrays in PHP, such as Array (), or arr[] to implement the definition of arrays, let me give you a detailed description of the PHP array definition of the various techniques of the PHP array is a heavy ...

  • 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.