0

i found an issue in a webservice using php, i have an event(one record) in my db, that is for multiple days like,

2013-09-17 00:00:00 to 2013-09-20 23:59:59

for four days, i want to print it four times with four different date like ,

event_date =  2013-09-17 00:00:00
event_date =  2013-09-18 00:00:00
event_date =  2013-09-19 00:00:00
event_date =  2013-09-20 00:00:00

but it always prints the last date, i am looping to print json object four times, it works fine, but always prints the last date in each object,

My expected output shoud be,

{

"id": "472",
"event_date": "2013-09-17 00:00:00",
"event_type": "MULTIPLE_DAYS",
"multiple_days_start_date": "2013-09-17 00:00:00",
"multiple_days_end_date": "2013-09-20 23:59:59"

}, {

"id": "472",
"event_date": "2013-09-18 00:00:00",
"event_type": "MULTIPLE_DAYS",
"multiple_days_start_date": "2013-09-17 00:00:00",
"multiple_days_end_date": "2013-09-20 23:59:59"

}, {

"id": "472",
"event_date": "2013-09-19 00:00:00",
"event_type": "MULTIPLE_DAYS",
"multiple_days_start_date": "2013-09-17 00:00:00",
"multiple_days_end_date": "2013-09-20 23:59:59"

}, {

"id": "472",
"event_date": "2013-09-20 00:00:00",
"event_type": "MULTIPLE_DAYS",
"multiple_days_start_date": "2013-09-17 00:00:00",
"multiple_days_end_date": "2013-09-20 23:59:59"

},

here is my json object output,

{

"id": "1",
"event_date": "2013-09-19 00:00:00",
"event_type": "MULTIPLE_DAYS",
"multiple_days_start_date": "2013-09-17 00:00:00",
"multiple_days_end_date": "2013-09-20 23:59:59"

}

here is my code,

(foreach $events as $event)
if($event->event_type == 'MULTIPLE_DAYS') {
    $mul_start_date = substr($event->multiple_days_start_date, 8,2);
    $mul_end_date = substr($event->multiple_days_end_date, 8,2);
   for($mul_start_date; $mul_start_date<=$mul_end_date;) {
    $event->event_date = substr_replace($event->multiple_days_start_date, $mul_start_date, 8,2);
    $events[] = $event;
    $mul_start_date++;
   }
}
echo json_encode($events);

Any help please,

1
  • $event is an object, and its reference is stored in the array $events. Therefore, when the object is added to the array and the object is modified, all (same) other objects in the array seems to be modified. However, I don't know how to resolve this problem prettily... :( Commented Sep 18, 2013 at 6:28

3 Answers 3

1
$events[] = $event;

That's the issue in your code.

$event->event_date contains the date you need and that's what you need to push into the result array. But as of now, the entire JSON object is being pushed.

Change it to:

$events[] = $event->event_date;

Demo!


UPDATE

After the discussion, it seems like you're trying to re-encode the JSON object to JSON form. If that's the case, JiminP correctly addressed the issue in comments:

$event is an object, and its reference is stored in the array $events. Therefore, when the object is added to the array and the object is modified, all (same) other objects in the array seems to be modified.

So, to fix the issue, you just need to type-cast the $event object into an array:

$events[] = (array) $event;

This may not be the best solution though. Anyway, your corrected code should now look like:

if($event->event_type == 'MULTIPLE_DAYS') 
{
    $mul_start_date = substr($event->multiple_days_start_date, 8,2);

    $mul_end_date = substr($event->multiple_days_end_date, 8,2);

    for($mul_start_date; $mul_start_date<=$mul_end_date;) 
    {
        $event->event_date = substr_replace($event->multiple_days_start_date, $mul_start_date, 8,2);
        $events[] = (array) $event;
        $mul_start_date++;

    }
    $encoded = json_encode($events, JSON_PRETTY_PRINT);
    echo $encoded;
}

Note: JSON_PRETTY_PRINT (available since PHP 5.4.0) is just used for formatting the output. The encoded JSON is stored in a variable, and is then echoed.

Demo!

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

1 Comment

@IrfanAhmed: Oh, are you trying to re-create the JSON with new event_dates ?'
1

Try to type cast object to array.

I hope this could help: change

$events[] = $event; 

to:

$events[] = (array) $event;

Comments

0

Try this. Store your object into array variable. Then you print the array

    for($mul_start_date; $mul_start_date<=$mul_end_date;) {
        $event->event_date = substr_replace($event->multiple_days_start_date, $mul_start_date, 8,2);
        $everntdate[] = $event->event_date;        
        $mul_start_date++;
       }
echo "<pre>";
print_r($everntdate);

your output like this format

Array
(
    [0] => 2013-09-17 00:00:00
    [1] => 2013-09-18 00:00:00
    [2] => 2013-09-19 00:00:00
    [3] => 2013-09-20 00:00:00
)

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.