如何在 Laravel 控制器中使用 "for loop" 而不是 "foreach loop"?
How to use "for loop" instead of "foreach loop" in Laravel controller?
$client = Client::all();
$infos = array();
$counter = 0;
$a_name = "ayman";
foreach($client as $cl) {
if ($cl->name == $a_name)
//code
}
我可以用那个吗?但它不起作用!!
$count_client = $Client::count();
for($i = 0; $i<$count_client; $i++) {
if ($cl[i]->name == $a_name) {
//code
}
}
这是一个简单的代码,但会知道如何使用 "for loop" 而不是 "foreach loop" 来将数据设置为数组。
已将您的 foreach 转换为 for 循环
$client = Client::all();
$count = count($client);
$a_name = "ayman";
for($i=0; $i < $count ; $i++) {
if ($client[$i]->name == $a_name)
//your code
}
}
试试这个....
for ($x = 0; $x < count($client); $x++)
{
if($client[x]->name == $a_name)
//.........
}
你能做到
$client = Client::all();
$a_name = "ayman";
for($i=1; $i <= count($client) ; $i++)
{
if ($client[$i]->name == $a_name)
{
//your code
}
}
$client = Client::all();
$infos = array();
$counter = 0;
$a_name = "ayman";
foreach($client as $cl) {
if ($cl->name == $a_name)
//code
}
我可以用那个吗?但它不起作用!!
$count_client = $Client::count();
for($i = 0; $i<$count_client; $i++) {
if ($cl[i]->name == $a_name) {
//code
}
}
这是一个简单的代码,但会知道如何使用 "for loop" 而不是 "foreach loop" 来将数据设置为数组。
已将您的 foreach 转换为 for 循环
$client = Client::all();
$count = count($client);
$a_name = "ayman";
for($i=0; $i < $count ; $i++) {
if ($client[$i]->name == $a_name)
//your code
}
}
试试这个....
for ($x = 0; $x < count($client); $x++)
{
if($client[x]->name == $a_name)
//.........
}
你能做到
$client = Client::all();
$a_name = "ayman";
for($i=1; $i <= count($client) ; $i++)
{
if ($client[$i]->name == $a_name)
{
//your code
}
}