我在尝试取回单个数组时遇到了困难,然后我可以将其作为 JSON 返回。
该模型:
class CoverageLimit extends Eloquent {
protected $table = 'webapp_limit_tbl';
public static function index($code,$plan_type,$income_tier){
$data = CoverageLimit::where('limitcode', $code)
->where('plantypecode', $plan_type)
->where('incometiercode', $income_tier)
->get(array('limitcode','limit_desc'));
return print_r($data->toArray());
}
}
控制器:
$limits = array();
foreach($pieces as $coverage_limit) {
$limits = array_merge($limits, coveragelimit::index($coverage_limit,$plan_type,$income_tier));
//this now works thanks to suggestion below
}
return $limits;
回报:
Array
(
[0] => Array
(
[limitcode] => L0001
[limit_desc] => $1M per claim / $2M annual aggregate
)
)
Array
(
[0] => Array
(
[limitcode] => L0002
[limit_desc] => $2M per claim/ $2M annual aggregate
)
)
[true,true]
目标是通过 json_encode($limits) 输出以下 JSON 数据:
[{"limitcode":"L0001","limit_desc":"$1M per claim / $2M annual aggregate"},{"limitcode":"L0002","limit_desc":"$2M per claim / $2M annual aggregate"}
]
我认为我需要一个像下面这样的单个数组来实现这一点,但我似乎无法实现它。
Array
(
[0] => Array
(
[limitcode] => L0001
[limit_desc] => $1M per claim / $2M annual aggregate
),
[1] => Array
(
[limitcode] => L0002
[limit_desc] => $2M per claim/ $2M annual aggregate
)
)
我将不胜感激有关如何将此返回合并到单个数组中以获取有效 json 输出的任何帮助。或者,如果有一种方法可以将它从 foreach 作为一个数组返回。谢谢。