All individual subscriptions are 50% off this week only!

BenBermingham
5 months ago

Converting collections to JSON

Posted 5 months ago by BenBermingham

I am trying to convert a collection which has over 300 entries into a JSON format to eventually be saved to a file.

        foreach($clients as $item) {
            $cl_array = explode(":", trim($item));
            $combined = @array_combine($tpl_array, $cl_array);
            if(!$combined) {
                continue;
            }
            if($combined && is_array($combined)) {
                $combined["planned_remarks"] = wordwrap($combined["planned_remarks"], 40);
                $combined["planned_route"]   = wordwrap($combined["planned_route"], 40);
                $combined["atis_message"]    = wordwrap($combined["atis_message"], 40);
                $clients_final[]             = $combined;
            }
        } 
        $json = json_encode($clients_final, 128, 2048);
        dd($json);
        return view('home');

When I load the page it returns false, but if I only encode 1 entry from $clients_final it works fine. Any ideas on how to fix this so I can covert the whole collection to an array.

Please sign in or create an account to participate in this conversation.