PHP Loop two-dimensional array ____php

Source: Internet
Author: User

PHP Code:

<?php
$arr 1=array (100,200,300,400);
$arr 2=array ("num" =>100, "name" => "Liuxy", "score" =>98);
Print_r ($arr 1);
echo "<br>";
Print_r ($arr 2);
?>


The output of the above code is as follows:
Array ([0] => [1] => [2] => [3] => 400)
Array ([num] => [name] => Liuxy [score] => 98)
To define a two-dimensional array, you can nest the array function, similar to C, and consider each row of a two-dimensional array as a one-dimensional array, which can be different from one dimension array for each row. Such as:
Copy PHP content to clipboard
PHP Code:
$arr =array (
"Row1" =>array (100,200,300,400),
"Row2" =>array ("num" =>100, "name" => "Liuxy", "Score" =>98)
);


The access to the PHP array element is similar to C, which is the form of the array name [key name].
The traversal of the PHP array can be implemented by a foreach statement, which is formatted as follows:
foreach (array name as variable name) data element processing statement;
Translation into C language is:
for (i=0;i<n; i++) {variable name =a; other processing statements containing "variable name"}
foreach is equivalent to the previous for (i=0;i<n; i++)
As is equivalent to the equals sign between "variable name" and "a", you get it.
For example:
Copy PHP content to clipboard
PHP Code:

<?php
$arr =array (1,2,3,4,5,6);
foreach ($arr as $value) echo "$value <br>";
?>


What is the output result. Think about it.
If you want to also output the corresponding key name, you can use this form:
foreach ($arr as $key => $value) echo "The element value named $key is $value";
Traversing a two-dimensional array in C we typically use a double for loop, and, correspondingly, a foreach in PHP can be nested, analyzing the following program and you'll see:
Copy PHP content to clipboard
PHP Code:

<?php
$arr =array (
"Row1" =>array (100,200,300,400),
"Row2" =>array ("num" =>100, "name" => "Liuxy", "Score" =>98)
);

foreach ($arr as $key => $value)
{
echo "$key => $value";
echo "<br>";
foreach ($value as $k => $var) echo "$k => $var <br>";
}
?>


PHP's array elements can be dynamically grown, which is not dared to think in C. Adding an element to an array is simple, just like accessing the elements of an element, except that the key name is new or empty, such as $arr [new]=3; or $arr []=3, the result is that PHP $arr add an element with a key named new to its value of 3, and if the key is empty, the maximum value of the current integer key plus 1 is assigned to the new element as the default key name. PHP's dynamic growth array is sometimes convenient, but it also brings pitfalls, such as when we attempt to modify the existing element value and write the wrong key name, it becomes a new element, and such a logic error, the system will not complain.
Can grow can be reduced, to delete an array element, you can use the unset function, such as the deletion of the new element above, can be written as unset ($arr [new]). You can also use the unset function to remove the entire array structure, such as unset ($arr), and note that this is different from deleting all the array elements individually, which also retains the structure of the array. Figuratively speaking, the latter is deserted, but the building is still, the former even the building has been dismantled. It is interesting to delete all the elements of an array if a new element is added, its default key name is incremented before the array's maximum key names increment, to start from 0, you can use the Array_values function to reset, such as $arr=array_values ($arr).
To remind you that PHP is an array of sorting, find, Merge, Split provides a large number of functions, use these basic algorithms, do not have to go over the data structure of textbooks, hehe ...

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.