0

I am using PHP 5.5.12.

I have an array like:

Array
(
    [0] => Array
        (
            [user_id] => 3
            [medicine_id] => 1
            [time] => Array
                (
                    [0] => stdClass Object
                        (
                            [event_type] => before_breakfast
                            [time] => 07:00:00
                        )

                    [1] => stdClass Object
                        (
                            [event_type] => after_breakfast
                            [time] => 07:30:00
                        )

                )

        )

    [1] => Array
        (
            [user_id] => 3
            [medicine_id] => 2
            [time] => Array
                (
                    [0] => stdClass Object
                        (
                            [event_type] => before_lunch
                            [time] => 13:00:00
                        )

                    [1] => stdClass Object
                        (
                            [event_type] => after_lunch
                            [time] => 14:00:00
                        )

                )

        )

    [2] => Array
        (
            [user_id] => 3
            [medicine_id] => 3
            [time] => Array
                (
                    [0] => stdClass Object
                        (
                            [event_type] => before_dinner
                            [time] => 20:00:00
                        )

                    [1] => stdClass Object
                        (
                            [event_type] => after_lunch
                            [time] => 21:00:00
                        )
                )
        )
)

I want to json_encode() the field time of each root level.

I tried using:

foreach ($user_medicine_times as $user_medicine_key => $user_medicine_value) {
    $user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}

and:

foreach ($user_medicine_times as $user_medicine_key => &$user_medicine_value) {
    $user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}

But using print_r($user_medicine_value), it returns the same array.

I want the result to be as follows:

Array
(
    [0] => Array
        (
            [user_id] => 3
            [medicine_id] => 1
            [time] => "[{"event_type":"before_breakfast","time":"07:00:00"},{"event_type":"after_breakfast","time":"07:30:00"}]"

        )

    [1] => Array
        (
            [user_id] => 3
            [medicine_id] => 2
            [time] => "[{"event_type":"before_lunch","time":"13:00:00"},{"event_type":"after_lunch","time":"17:00:00"}]"

        )

    [2] => Array
        (
            [user_id] => 3
            [medicine_id] => 3
            [time] => "[{"event_type":"before_dinner","time":"20:00:00"},{"event_type":"after_lunch","time":"17:00:00"}]"

        )

)

How can I achieve this result?

5
  • 1
    You've asked a similar question today and apparently deleted that question, why ?! We have bounty's if you think your question didn't receive enough attention. Commented Jul 20, 2015 at 12:45
  • 1
    Its better to make a new array consisting only the values of [time] and then use json_encode Commented Jul 20, 2015 at 12:46
  • The question I deleted was to something related to filter an array and replace specific key value, I've achieved it. This is regarding convert specific key value to JSON. Bounties are eligible after 2 days. I cannot wait for that long if working on deadline. Also, without providing existing solution, down vote is arrogance I think that I've deleted it. Commented Jul 20, 2015 at 12:47
  • 2nd variant shold work Commented Jul 20, 2015 at 12:48
  • @Dev its an array of object not an array Commented Jul 20, 2015 at 12:57

4 Answers 4

1

I have read your question earlier and prepared the answer but you removed it before i paste the answer. Anyways here is the solution

function outer(&$val, $key) {
    $val['time'] = json_encode($val['time']);
}
array_walk($your_array, 'outer');
print_r($your_array);
Sign up to request clarification or add additional context in comments.

Comments

0

You can replace your foreach loop's content with something like this:

foreach ($user_medicine_times as $user_medicine_key => $user_medicine_value) {
    $user_medicine_times[$user_medicine_key]['time'] = json_encode($user_medicine_value['time'], true);
}

Comments

0

Maybe the json encode fails because your time array contains an stdClass Object. Try to convert this like that :

$result = array();
foreach ($user_medicine_value['time'] as $value) {
    $result['event_type'] = $value->event_type;
    $result['time'] = $value->time;
}
$user_medicine_value['time'] = $result;

Comments

0

Because, in every iteration, the value is not being saved anywhere,

You have two options here, either make new array having time key with json_encode() or pass the value by reference as shown below.

foreach ($user_medicine_times as $user_medicine_key => &$user_medicine_value) {
                                                       ^
    $user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}

1 Comment

Because of &$user_medicine_value it gives error : Invalid argument supplied for foreach(). And using true in json_encode also not affecting the provided array. It returns the same array I've used as an input.

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.