PHP foreach Traversal

Source: Internet
Author: User
Tags php foreach

foreach ($facility _list[' data ') as $facility) {
Processing statements
}

The first format iterates through the given array of Array_expression_r_r. In each loop, the value of the current cell is assigned to the $value and the pointer inside the array is moved forward one step (so the next cell in the next loop will be taken).
The second format does the same thing, except that the key value of the current cell is assigned to the variable $key in each loop.
Note: When foreach starts executing, the pointer inside the array automatically points to the first cell. This means that you do not need to call Reset () before the Foreach loop.
Note: Also note that foreach is manipulating a copy of the specified array, not the array itself. So even with each () construct, the original array pointer does not change, and the value of the array cell is unaffected.
Note: foreach does not support the ability to suppress error messages with "@".

Suppose the requested data format:

$facility _list variable values are:

Array (1) {["Data"]=> Array (3) {[0]=> Array (4) {["facility_id"]=> string (1) "1" ["Facility_name"]=> string (9 "Jiaxing Warehouse" ["Schedule_mode"]=> string "manual_schedule_auto_shipping" ["Facility_type"]=> string (1) "1"} [1]= > Array (4) {["facility_id"]=> string (1) "2" ["Facility_name"]=> string (9) "Guangzhou warehouse" ["Schedule_mode"]=> string ( "Manual_schedule_auto_shipping" ["Facility_type"]=> string (1) "1"}}}

Here is an array of arrays (1), so we need to get the data inside, we need to go to the value in this array data point,

$facility _list[' data ') variable value is:

Array (3) {[0]=> Array (4) {["facility_id"]=> string (1) "1" ["Facility_name"]=> string (9) "Jiaxing warehouse" ["Schedule_mode" ]=> string "manual_schedule_auto_shipping" ["Facility_type"]=> string (1) "1"} [1]=> Array (4) {["Facility_ ID "]=> string (1)" 2 "[" Facility_name "]=> string (9)" Guangzhou warehouse "[" Schedule_mode "]=> string" Manual_schedule_auto _shipping "[" Facility_type "]=> string (1)" 1 "}}

The following array is obtained by this statement, which is equivalent to entering an internal array from an external array, containing an array, and iterating over each value on the inner array, for example:
$facility [' facility_id '] variable values are:

String (1) "1"

Example:

$facility _list=$this->common->getfacilitylist (); $facility _id=$this->getinput (' facility_id '); if(isset($facility _id)) {            $cond[' facility_id '] =$facility _id; foreach($facility _list[' Data '] as $facility){                if($facility[' facility_id ']==$facility _id){                    $cond[' Facility_type ']=$facility[' Facility_type ']; }            }        } Else {            $cond[' facility_id '] =$facility _list[' Data '] [0] [' facility_id ']; $cond[' facility_type '] =$facility _list[' Data '] [0] [' Facility_type ']; }

PHP foreach Traversal

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.