Php array declaration, traversal, array global variables

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff to enter foreach ($ useras $ key $ val) $ val $ key is a custom variable {echo $ key .. $ val.br;} foreach traverses multi-dimensional arrays $ infoarray (userarray ($ user [0] array (1, zansan, 10, nan), $ user

Welcome to the Linux community forum and interact with the 2 million technical staff> enter foreach ($ user as $ key = $ val) // $ val $ key are all custom variables {echo $ key. "= ". $ val. "br";} // foreach traverse multi-dimensional arrays $ info = array ("user" = array (// $ user [0] array (1, "zansan", 10, "nan"), // $ user

Welcome to the Linux community forum and interact with 2 million technicians>

Foreach ($ user as $ key => $ val) // $ val $ key is a custom variable

{

Echo $ key. "=====>". $ val ."
";

}

// Foreach traverses multi-dimensional arrays

$ Info = array (

"User" => array (

// $ User [0]

Array (1, "zansan", 10, "nan "),

// $ User [1] [1]

Array (2, "lisi", 20, "nv"), // $ user [1]

// $ User [2]

Array (3, "wangwu", 30, "nan ")

),

"Score" => array (

Array (1,100, 90, 80 ),

Array (2, 99, 88, 11 ),

Array (3, 10, 50, 88)

),

"Connect" => array (

Array (1, '123', 'aaa @ bbb.com '),

Array (2, '123', 'bbb @ ccc.com '),

Array (3, '123', 'ccc @ ddd.com ')

)

);

Foreach ($ info as $ tableName => $ table)

{

Echo'

Echo'

Foreach ($ table as $ row)

{

Echo'

Foreach ($ row as $ col)

{

Echo'

}

Echo'

}

Echo'

'; '; '; '; ';
'. $ TableName .'
'. $ Col .'
';

}

// Use of each ()

$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");

$ A = each ($ user); // Array ([1] => 1 [value] => 1 [0] => id [key] => id) the default value is the value of the first element.

Print_r ($ );

$ B = each ($ user );

Print_r ($ B); // Array ([1] => zhangsan [value] => zhangsan [0] => name [key] => name) each time, traverse one

$ C = each ($ user );

Print_r ($ c); // Array ([1] => 10 [value] => 10 [0] => age [key] => age)

$ D = each ($ user );

Print_r ($ d); // Array ([1] => nan [value] => nan [0] => sex [key] => sex)

$ E = each ($ user );

Var_dump ($ e); // bool (false) value returned when no element exists

// Each () and while Traversal

$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");

While ($ arr = each ($ user ))

{

// Echo $ arr [0]. "===>". $ arr [1]."
"; // Display the key (subscript) and value through 0, 1

Echo $ arr ["key"]. "==>". $ arr ["value"]."
"; // Key and value are used to display the key value

}

// Use the list () function

List ($ name, $ age, $ sex) = array ("zhangsan", 10, "nnnnn ");

Echo $ name ."
";

Echo $ age ."
";

Echo $ sex ."
";

// ANOTHER METHOD

List (, $ sex) = array ("zhangsan", 10, "nnnnn ");

Echo $ sex ."
"; // Only convert gender to variable

// Ip judgment

$ Ip = "192.168.1.128 ";

List (, $ d) = explode (".", $ ip); // explode is separated by. And an array is returned.

Echo $ d; // retrieve 128

// List () can only receive indexed arrays.

$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");

List ($ key, $ value) = each ($ user); // Array ([1] => 1 [0] => id) assign values to the parameters in the list according to the order of the index, so the values are 0 and 1.

Echo $ key. "--->". $ value;

// While list () each () Combination

$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");

While (list ($ key, $ value) = each ($ user ))

{

Echo $ key. "--->". $ value ."
";

}

// The solution is to display only once for multiple cycles

// Use the internal pointer control function of the array

// Next (array); move the array pointer to the next

// Prev (array); move the array pointer to the previous one

// Reset (array); move the array pointer to the first one (reset)

// End (array); the array pointer moves to the last one

// Current (array); obtains the value of the current element. The element pointed to by the index group pointer when the current element is present.

// Key (array); obtains the key value (subscript) of the current element)

$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");

While (list ($ key, $ value) = each ($ user ))

{

Echo $ key. "--->". $ value ."
";

}

// Here, move the array pointer to the first loop below to output

// Reset ($ user)

While (list ($ key, $ value) = each ($ user) // The loop jumps out directly because each () returns false to the last one.

{

Echo $ key. "--->". $ value ."
";

}

While (list ($ key, $ value) = each ($ user) // The loop jumps out directly because each () returns false to the last one.

{

Echo $ key. "--->". $ value ."
";

}

Echo current ($ user). "====>". key ($ user );

?>

[1] [2] [3]

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.