1

I am re-encoding some json objects in a webservice , i tested locally, all stuff is pretty fine,

encoded result locally ,

but when i upload it on a live server it changed my array to a single object containing all abjects with index as,

encoded result live server which creates parsing error,

here is my code

foreach ( $events as $eventkey => $event ) {
    if ( $event->recurring_type == "DAILY" ) {
        $date = new DateTime( $event->recurring_start_date );
        $new_event = $event;
        if ( $event->recurring_start_date < $event->recurring_end_date ) {
        while ( $new_event->recurring_start_date < $new_event->recurring_end_date ) {
           $new_event->event_date = $new_event->recurring_start_date;
               $events[] = (array) $new_event;
           $new_event->recurring_start_date = $date->modify('+1 day')->format('Y-m-d H:i:s');
            }
         } else {
        unset( $events[$eventkey] ); // used to avoid duplicate result with same data event_date,
         }
     }

If i remove this line

unset( $events[$eventkey] );

then, results appear fine at online too. but i need this! I cant understand the exact problem!
Please help

2 Answers 2

1

With unset(), your array becomes an associative array and therefore encoded as an object in JSON.

My two cent : instead of unset(), use array_filter() AFTER your loop.

http://php.net/manual/fr/function.array-filter.php

Sign up to request clarification or add additional context in comments.

Comments

1
<?php

$arr = array('a' , 'b' , 'c' , 'd' , 'e' );
unset($arr[0]);
echo json_encode($arr);//here o/p as object {"1":"b","2":"c","3":"d","4":"e"}
$arr= array_values($arr);
echo json_encode($arr);//["b","c","d","e"]

code pad

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.