你可以嘗試這樣的事情?
<?php
$cars=array("Volvo1","Volvo2","Volvo3","Volvo4","Volvo5","Volvo6","BMW1","BMW2","BMW3","BMW4","BMW5","BMW", "Toyota1","Toyota2","Toyota3","Toyota4","Toyota5","Toyota6");
$arrlength=count($cars);
for($x = 0;$x < $arrlength; $x++)
{
// substr(string, start, length)
if(substr($cars[$x], 0, 1) === 'T')
{
echo $cars[$x];
echo "<br>";
}
// or use this
// 0 is first character of the string
if($cars[$x][0] === 'T')
{
echo $cars[$x];
echo "<br>";
}
}
?>
但更好的方法是,而不是使用for
循環只使用一個foreach
循環,如下圖所示,
<?php
$cars=array("Volvo1","Volvo2","Volvo3","Volvo4","Volvo5","Volvo6","BMW1","BMW2","BMW3","BMW4","BMW5","BMW", "Toyota1","Toyota2","Toyota3","Toyota4","Toyota5","Toyota6");
foreach($cars as $car)
{
// substr(string, start, length)
if(substr($car, 0, 1) === 'T')
{
echo $car;
echo "<br>";
}
// or use this
// 0 is first character of the string
if($car[0] === 'T')
{
echo $car;
echo "<br>";
}
}
?>