Merging PHP Arrays for JSON output -
i'm having difficult time trying single array, can return json.
the model:
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()); } }
the controller:
$limits = array(); foreach($pieces $coverage_limit) { $limits = array_merge($limits, coveragelimit::index($coverage_limit,$plan_type,$income_tier)); //this works suggestion below } return $limits;
returns:
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]
the goal output following json data via json_encode($limits):
[{"limitcode":"l0001","limit_desc":"$1m per claim / $2m annual aggregate"},{"limitcode":"l0002","limit_desc":"$2m per claim / $2m annual aggregate"}
]
i think need single array below in order accomplish this, can't seem make happen.
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 ) )
i grateful on how merge return single array valid json output. or if there way return foreach 1 array. thanks.
first, not use return print_r($data->toarray());
that incorrect. return $data->toarray()
, should work fine.
the problem print_r returns boolean , outputs stdout text representation of data structure. why see annoying [true, true] @ end of output example. array attempting json_encode array 2 values of true returned print_r.
Comments
Post a Comment