我有一个阵列
$test = array('one', 'two', 'three', 'four', 'five');
Run Code Online (Sandbox Code Playgroud)
我取消了两个和四个(因为我真的很不喜欢偶数)
unset($test[array_search('two', $test)]);
unset($test[array_search('four', $test)]);
Run Code Online (Sandbox Code Playgroud)
我留下了一个阵列
$test = array(
[0] => 'one',
[2] => 'three',
[4] => 'five'
);
Run Code Online (Sandbox Code Playgroud)
现在我需要循环遍历该数组并获取值和正确的键.我想要展示
0 = one
2 = three
4 = five
Run Code Online (Sandbox Code Playgroud)
我应该用什么来实现这一目标?我尝试使用标准
for($i=0; $i<count($test); $i++)
Run Code Online (Sandbox Code Playgroud)
但这没有效果,因为它会循环并给我0 1 2而不是0 2 4.有什么建议吗?
您可以使用foreach循环
foreach($test as $key => $value) {
echo "$key = $value";
}
Run Code Online (Sandbox Code Playgroud)
在php.net手册上阅读foreach循环
| 归档时间: |
|
| 查看次数: |
93 次 |
| 最近记录: |