How to use foreach to operate on array? Detailed explanation of foreach operation array instance

伊谢尔伦
Release: 2023-03-11 09:06:01
Original
1325 people have browsed it

foreach() has two uses:

foreach(array_name as $value) 
{ 
statement; 
}
Copy after login

The array_name here is the array name you want to traverse, each timeLoop, the value of the current element of the array_name array is assigned to $value, and the subscript inside the array moves down one step, that is, the next element is obtained in the next loop.

foreach(array_name as $key => $value) 
{ 
statement; 
}
Copy after login

The difference between this and the first method is that there is an extra $key, that is, in addition to assigning the value of the current element to $value, the key value of the current element will also be assigned in each loop. Give variable$key. The key value can be a subscript value or a string. For example, "0" in book[0]=1, "id" in book[id]="001".
Program Example 1:

<?php 
/*-------------------------------------------------------------------------*/ 
/* foreach example 1: value only */ 
echo "foreach example 1: value only ".&#39;<br />&#39;; 
$a = array(1, 2, 3, 17); 
foreach ($a as $v) { 
echo "
Current
 value of ".$a.":". $v."<br />"; 
} 
?>
Copy after login

// Running results
foreach example 1: value only
Current value of $a: 1
Current value of $a: 2
Current value of $a: 3
Current value of $a: 17

2 The code is as follows:

/*-------------------------------------------------------------------------*/ 
/* foreach example 2: value (with key printed for illustration) */ 
echo &#39;<br />&#39;.&#39;<br />&#39;."foreach example 2: value (with key printed for illustration) ".&#39;<br />&#39;; 
$a = array(1, 2, 3, 17); 
$i = 0; /* for illustrative purposes only */ 
foreach ($a as $v) { 
echo ""$a[$i] => $v".&#39;<br />&#39;; 
$i++; 
} 
// 程序运行结果 
foreach example 2: value (with key printed for illustration) 
$a[0] => 1 
$a[1] => 2 
$a[2] => 3 
$a[3] => 17
Copy after login

3 The code is as follows:

/*-------------------------------------------------------------------------*/ 
/* foreach example 3: key and value */ 
echo &#39;<br />&#39;.&#39;<br />&#39;."foreach example 3: key and value ".&#39;<br />&#39;; 
$a = array( 
"one" => 1, 
"two" => 2, 
"three" => 3, 
"seventeen" => 17 
); 
foreach ($a as $k => $v) { 
echo ""$a[$k] => $v".&#39;<br />&#39;; 
} 
// 程序运行结果 
foreach example 3: key and value 
$a[one] => 1 
$a[two] => 2 
$a[three] => 3 
$a[seventeen] => 17
Copy after login

4 The code is as follows:

/*-------------------------------------------------------------------------*/ 
/* foreach example 4: multi-dimensional arrays */ 
echo &#39;<br />&#39;.&#39;<br />&#39;."foreach example 4: multi-dimensional arrays ".&#39;<br />&#39;; 
$a = array(); 
$a[0][0] = "a"; 
$a[0][1] = "b"; 
$a[1][0] = "y"; 
$a[1][1] = "z"; 
foreach ($a as $v1) { 
foreach ($v1 as $v2) { 
echo "$v2"n"; 
} 
} 
// 程序运行结果 
foreach example 4: multi-dimensional arrays 
a b y z
Copy after login

5 The code is as follows:

/*-------------------------------------------------------------------------*/ 
/* foreach example 5: dynamic arrays */ 
echo &#39;<br />&#39;.&#39;<br />&#39;."foreach example 5: dynamic arrays ".&#39;<br />&#39;; 
foreach (array(1, 2, 3, 4, 5) as $v) { 
echo "$v"n"; 
} 
// 程序运行结果 
foreach example 5: dynamic arrays 
1 2 3 4 5
Copy after login

can also be used like this:

$messageNav[&#39;首页&#39;] =ROOT_PATH; 
$messageNav[&#39;人才交流&#39;] ="#" 
$messageNav[&#39;动态专栏&#39;] ="hragent/cn/" 
<?php $i = 0;foreach ($messageNav as $key=>$value):?> 
<?php if ($i != count($messageNav) - 1):?> 
<a href="<?=$value?>"><?=$key?></a>> 
<?php else:?> 
<a href="<?=$value?>" class="onlink"><?=$key?></a> 
<?php endif;?> 
<?php $i++;endforeach;?>
Copy after login


The above is the detailed content of How to use foreach to operate on array? Detailed explanation of foreach operation array instance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template