public static function json_add($template, $data) { $instance = new self(); $valid_api_template = $instance->json_api_call($template); if ($valid_api_template == false) { if ($template[0] != '[') { // check to see if json have square bracket syntax $template = "[" . $template . "]"; } $template_array = json_decode($template, true); //echo sizeof($template_array[0]); //echo sizeof($data); //print_r($template_array[0]); $i = sizeof($data); $newindex = 0; if (sizeof($template_array[0]) == sizeof($data)) { foreach ($template_array[0] as $index => $value) { // echo $index; if ($value == "*empty*") { $template_array[0][$index] = $data[sizeof($data) - $i]; $i = $i - 1; } else { if ($newindex == 0) { $newindex = sizeof($template_array); } $template_array[$newindex][$index] = $data[sizeof($data) - $i]; $i = $i - 1; } } } $template = json_encode($template_array); } else { $template = $valid_api_template; } return $template; }