打印使用一个Foreach循环多维数组只 [英] Printing a multi dimensional array using one Foreach loop only
本文介绍了打印使用一个Foreach循环多维数组只的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最近,我一直在考虑接受记者采访时,许多问题都问了,但有一个问题我不能回答这是以下内容:
Recently I've been given an interview where many question were ask, but there was one question I could not answer which was the following:
打印使用一个的foreach
循环多维数组只。
Printing a multi dimensional array using one foreach
loop only.
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
记者问我打印只使用一个的foreach
循环上述阵列。任何人都可以给我答案吗?
Interviewer asked me to print the above array using only one foreach
loop. Can anybody give me answer for this?
推荐答案
在这里你去
$cars = array ( array("Volvo",22,18), array("BMW",15,13),array("Saab",5,2), array("Land Rover",17,15) );
foreach($cars as $car) {
print("Name: ".$car[0]."\n");
print("Number 1: ".$car[1]."\n");
print("Number 2: ".$car[2]."\n");
}
显然可以可以作出检查以确定在内部阵列的其它键被设置。不过看来你的数组将始终包含所有的按键。
Obviously could could make checks to see if the other keys in the inner arrays are set. But seems your array will always contains all the keys.
编辑:
$cars = array ( array("Volvo",22,18), array("BMW",15,13),array("Saab",5,2), array("Land Rover",17,15) );
foreach($cars as $car) {
$count = 0;
A:
if(isset($car[$count])) {
print($car[$count] . "\n");
$count +=1;
goto A;
}
}
这篇关于打印使用一个Foreach循环多维数组只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文