Numeric array, associative array, multidimensional array usage in PHP

Source: Internet
Author: User
Tags numeric


Numeric array
The numeric array stores each element with a numeric ID key.

You can use different methods to create an array of values:

Example 1
In this example, the ID key is assigned automatically:

The code is as follows Copy Code

$names = Array ("Peter", "Quagmire", "Joe");

Example 2
In this example, we manually assign the ID key:

The code is as follows Copy Code

$names [0] = "Peter";
$names [1] = "quagmire";
$names [2] = "Joe"; these ID keys can be used in scripts:

<?php

$names [0] = "Peter";
$names [1] = "quagmire";
$names [2] = "Joe";

echo $names [1]. "and". $names [2]. "Are". $names [0]. "' s Neighbors";
?>

Array sorting

(1) sort (array $array [, int sorttype])
Array to represent one of the arrays
<table class=dataintable><tbody><tr><td> SortType Value:</td><td><p> Sort_regular-Default. Processed in their original type (without changing the type). </P><P> Sort_numeric-handles the value as a number </P><P> sort_string-handles the value as a string &LT;/P&GT;&L T P> sort_locale_string-handles values as strings, based on local settings <sup style= "Font-family:verdana, Arial, Helvetica, Sans-serif" & Gt;*</sup>. </P></TD></TR></TBODY></TABLE>
(2) bool Rsort (array $array [, SortType]) function Reverse ordering (inverse value)
SortType ditto
(3) Random ordering of bool Shuffle () function
(4) Array array_reverse (array $array [, BOOL Preserve_keys]) in reverse order
Preserve the original key name when Preserve_keys is True
(5) array array_merge () merging arrays
(6) Array array_slice (array $array, int offset[,int Length[,boolpreserve_keys])
Offset is non-negative, the shift variable in this array starts from the end when it is negative
Length is positive, it means that there are many units in the sequence, the negative is the beginning of the end from the end of the first few points, if omitted from offset start to the last
Boolpreserve_keys ditto

Array Loop output

The code is as follows Copy Code
<pre class= "brush:php; Toolbar:true; Auto-links:true; " ><pre>
<?php
$shuzu =array (' A ' => ' wo ', ' B ' => ' ni ', ' C ' => ' ta ', ' d ' => ' php ', ' e ' => ' MySQL ');
echo "traverses the array using the Foreach function";
echo "<br/> $nbsp;<br/>";
foreach ($shuzu as $key => $value)
{
echo "$key representative: $value";
echo "<br/> $nbsp;<br/>";
}
?>
</pre></PRE>

Associative arrays

An associative array in which each of its ID keys is associated with a value.
Using a numeric array is not the best way to store data about a specific named value.
By associative arrays, we can use values as keys and assign values to them.
Example 1
In this case, we use an array to assign age to different people:

The code is as follows Copy Code
$ages = Array ("Peter" =>32, "quagmire" =>30, "Joe" =>34);

Example 2
This example is the same as Example 1, but shows another way to create an array:

The code is as follows Copy Code

$ages [' Peter '] = "32";
$ages [' quagmire '] = "30";
$ages [' Joe '] = ' 34 '; You can use the ID key in your script:
<?php

$ages [' Peter '] = "32";
$ages [' quagmire '] = "30";
$ages [' Joe '] = "34";

echo "Peter is". $ages [' Peter ']. "Years old."
?> the output of the above script:
Peter is years old.


The associative array is null-judged code, and then we'll talk about the processing of data nulls in detail.

  code is as follows copy code
<?php Tutorials
    $array = Array (0);
    if (empty ($array)) {
        echo "I'm empty n";
    }else{
        echo "I'm not empty. N";
   }
 & nbsp;  $array [' array ']= ' I am an array ';
    Print_r ($array);
    $array [' array1 ']= ' I am array 1 ';
    Print_r ($array);
    unset ($array [' array1 ']);
    Print_r ($array);
?

For more details please see: http://www.111cn.net/phper/php/39841.htm

Traverse

When traversing the list of users, simply query the username directly with Isset to see if it exists.

PHP Version Code:

The code is as follows Copy Code

<?php
$arrayHash = Array ();
foreach ($arrayN as $nameN) {
The bank executed N times.
$arrayHash [$nameN] = 1;
}

foreach ($arrayM as $keyM => $nameM) {
if (Isset ($arrayHash [$nameM])) {
The bank executed M times!
Unset ($arrayM [$keyM]);
}
}
return $arrayM;
?>

Multidimensional

Array

In a multidimensional array, each element in the primary array is also an array. Each element in the child array can also be an array, and so on.
Example 1
In this example, we created a multidimensional array with an automatically assigned ID key:

The code is as follows Copy Code
$families = array
(
"Griffin" =>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire" =>array
(
"Glenn"
),
"Brown" =>array
(
"Cleveland",
"Loretta",
"Junior"
)
If you output this array, it should look like this:
Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)

Example 2
let's try to show a single value in the above array:

The code is as follows Copy Code
echo "is". $families [' Griffin '][2].
"A part of the Griffin family"; The output of the above code:
Is Megan a part of the Griffin family?


Array Sorting

The code is as follows Copy Code

<?php
$array [] = Array ("Age" =>20, "name" => "Li");
$array [] = Array ("Age" =>21, "name" => "AI");
$array [] = Array ("Age" =>20, "name" => "CI");
$array [] = Array ("Age" =>22, "name" => "Di");

foreach ($array as $key => $value) {
$age [$key] = $value [' Age '];
$name [$key] = $value [' name '];
}

Array_multisort ($age, Sort_numeric,sort_desc, $name, SORT_STRING,SORT_ASC, $array);
Print_r ($array);
?>

More Array_multisort () can refer to http://www.111cn.net/phper/php/42696.htm

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.