我无法以所需的字符串格式打印多维数组。我尝试了很多,但是没有获得正确的字符串格式。
这是我要动态打印的多维数组:
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
这是尝试的代码
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
foreach ($cars as $car) {
foreach ($car as $c => $v) {
echo $v. '<hr>';
}
}
以下是理想的字符串格式,我想获取:
沃尔沃:有货:22,售出:18。
宝马:库存:15,售出:13。
Saab:有货:5,售出:2。
陆虎:有货:17,售出:15。
也许是这样?
foreach ($cars as $car) {
echo "$car[0]: In stock: $car[1], sold: $car[2] <hr>";
}
foreach ($cars as $car) {
[$name, $stock, $sold] = $car;
echo "{$name}: In stock: {$stock}, sold: {$sold}. <br>";
}