PHP Basics: Array definition and traversal

Source: Internet
Author: User

Array definition: Number of arrays a composite data, to use Print_r to print, you can load a number of values, each value with a different key to distinguish, (keys and values to correspond to each other), the key = the number on the box, the value of the box contents such as: $a = array (a=> ' Zhansan ',b=> ' Lisi ',c=> ' Wanglu '); Print_r ($a);

Additions and deletions of array elements
$a = array (1=> ' php ',2=> ' html ',3=> ' CSS ',4=> ' MySQL ', 5=>array (' a ' + = ' ajax ', ' b ' = ' jquery '));
Remove a value individually
echo $a [1];
Take out a two-dimensional array of value arrays up to 3 levels otherwise people can't understand.
echo $a [5][b];
Change the value of an array cell
$a [5][' a ']=linux;
Print_r ($a);
Add a unit
$a [6] = ' git ';
Delete an array cell
unset ($a [6]);
Print_r ($a);

Iterating through an array
Take out all the values in the array
$a = Array (' A ', ' B ', ' C ', ' d ', ' e ');
echo $a [0];
echo $a [1];
echo $a [2];

The loop index array can be so//count () that the number of elements in the array is computed.
for ($b =0; $b <count ($a); $b + +) {
echo $a [$b], ' <br/> ';} //

Traversing all values in an associative array
foreach ($a as $k = = $v);
Echo $k, ': ', $v, ' <br/> ';

Loop-only key Array_keys (): Returns all the key names in the array. There is no way for a foreach to loop out the key name in a single value,

All values are traversed in the associative array.
foreach ($a as $k = = $v) {
echo $a [$k] = $v, ' <br/> ';
}

Title: $stu = Array (' Lisi ' =>3, ' Wang ' =>5, ' Zhao ' =>6); Change the value of this array to twice times the original
foreach ($stu as $k = = $v) {
echo $stu [$k] = $v * *;
}
Print_r ($stu);

/* $arr = Array (' name ' = ' Lisi ', ' name1 ' = ' zhangsan ', ' name2 ' =>array (' name3 ' = ' ctx ', ' age ' = ' 18 '));
foreach ($arr as $k = + $v) {
    echo $k. ' ~ '. $v, ' <br/> ';
}
//How to loop out the see and value of the two-dimensional array */

/* $array = Array (1=> ' a ',2=> ' B ',3=> ' C ',4=> ' d ');
for ($i =1; $i <count ($array), $i + +) {
    echo $array [$i];
}
foreach ($array as $k = + $v) {
    echo $k. ' ~ '. $v, ' <br/> ';
} */

$array = Array (1=> ' a ',2=> ' B ',3=> ' C ',4=> ' d ');
/*foreach ($array as $k = + $v) {
    echo $v, ' <br/> ';
} The
//array does not have a way to loop out the keys separately
*/
/*print_r (Array_keys ($array));//array_keys This function is to take out all the keys in the number cell */

$stu = array (' Lisi ' =>3, ' Wanwu ' =>5, ' Zhaoliu ' =>6);
foreach ($stu as $k + $v) {
    $stu [$k] = $v *2;//Here's to note that this is a cell that modifies an array, so to modify the array cell format
}
Print_r ($stu);

$arr = Array (' name1 ' = ' lisi ', ' name2 ' = ' zhansan ', ' Name3 ' =>array (' name ' = ' = ' Wangwu ', ' age ' =>28, ' Genden ' = ' + ' man ');
Print the entire array
Print_r ($arr);
Echo ' <br/> ';

Take an array unit
Print_r ($arr [' name1 ']);

Echo ' <br/> ';

Take a unit of a two-dimensional array
Print_r ($arr [' Name3 '] [' Genden ']);
Echo ' <br/> ';

Modifying an array cell
$arr [' name1 '] = ' ctx ';
Print_r ($arr);
Echo ' <br/> ';
$arr [' Name3 '] [' age '] = 18;
Print_r ($arr);
Echo ' <br/> ';

Add an array unit
$arr [' name4 '] = ' ctxryqdnz ';
Print_r ($arr);
Echo ' <br/> ';

$arr [' Name3 '] [' height '] = 185;
Print_r ($arr);
Echo ' <br/> ';

Delete an array cell
unset ($arr [' name4 ']);
Print_r ($arr);
Echo ' <br/> ';
unset ($arr [' Name3 '] [' height ']);
Print_r ($arr);

PHP Basics: Array definition and 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.