当我使用foreach
Samp Object
(
[collection] => Array
(
[0] => Array
(
[fname] => "john",
[lname] => "peter"
)
[1] => Array
(
[fname] => "simon",
[lname] => "peter"
)
)
)
每当我访问该对象时,它必须自动找到数组,并且必须跳过"集合"
$samp = new Samp();
foreach ($samp as $row){
print_r($row)
}
结果必须像这样
Array
(
[fname] => "john",
[lname] => "peter"
)
Array
(
[fname] => "simon",
[lname] => "peter"
)
但每当我执行foreach
时,结果都是这样的
[collection] => Array
(
[0] => Array
(
[fname] => "john",
[lname] => "peter"
)
[1] => Array
(
[fname] => "simon",
[lname] => "peter"
)
)
我需要先做这个才能得到结果
$samp = new Samp();
foreach ($samp->collection as $row){
print_r($row)
}
或者这个
$samp = new Samp();
foreach ($samp as $row => $value){
print_r($value)
}
但是我想跳过这个集合我该怎么办? laravel框架工作做到了......我需要它到我的新班级
这是类代码
class Samp
{
public $collection;
public function samp($no)
{
$this->collection = array(["fname"=>"john","lname"=>"peter"],["fname"=>"simon","lname"=>"peter"]);
return $this;
}
public function pagination(){
}
}
所以我可以将对象用于另一个像这样的函数
$samp = new Samp();
foreach ($samp as $row){
echo $row['fname']." ".$row['lname'];
}
echo $samp->pagination();