我知道在PHP中printf
语句可以按照以下方式格式化字符串:
//my variable
$car = "BMW X6";
printf("I drive a %s",$car); // prints I drive a BMW X6,
然而,当我尝试使用printf
打印数组时,似乎没有一种格式化的方法。有人可以帮忙吗?
我知道在PHP中printf
语句可以按照以下方式格式化字符串:
//my variable
$car = "BMW X6";
printf("I drive a %s",$car); // prints I drive a BMW X6,
然而,当我尝试使用printf
打印数组时,似乎没有一种格式化的方法。有人可以帮忙吗?
// this doesn't work
printf('There is a difference between %s and %s', array('good', 'evil'));
与其
printf('There is a difference between %s and %s', 'good', 'evil');
function printf_array($format, $arr)
{
return call_user_func_array('printf', array_merge((array)$format, $arr));
}
使用方式如下:
$goodevil = array('good', 'evil');
printf_array('There is a difference between %s and %s', $goodevil);
它将会打印出:
There is a difference between good and evil
foreach
遍历它,然后使用值printf。例如:$cars = array('BMW X6', 'Audi A4', 'Dodge Ram Van');
foreach($cars as $car) {
printf("I drive a %s", $car);
}
我开BMW X6
我开Audi A4
我开Dodge Ram Van
是的
在你的HTML上放置这个:
<pre>
<?php
print_r ($your_array);
?>
</pre>
或者仅在你的代码位置:
print_r ($your_array);
printf不能递归处理数组。但是你可以这样做:
$cars=array('Saab','Volvo','Koenigsegg');
print_r($cars);
print_r
函数打印数组时发生的格式化吗? - user142162vprintf()
- salathe