1

I want to modify a JSON data format using PHP.

I have an array like this:

Array ( 
[0] => stdClass Object ( 
    [0] => Thu Apr 30 12:25:12 +0000 2015 ) 
[1] => stdClass Object ( 
     [0] => Wed Apr 15 21:57:05 +0000 2015 ) 
      )

I have tried the json_encode($data); but it is coming like this:

[{"0":"Thu Apr 30 12:25:12 +0000 2015"},{"0":"Wed Apr 15 21:57:05 +0000   2015"}]

But I want this format:

["Thu Apr 30 12:25:12 +0000 2015","Wed Apr 15 21:57:05 +0000   2015"]

what should i do ?

4
  • 2
    why you want like this. it will not going to decode next time. Commented May 29, 2015 at 14:16
  • 2
    you'd need to revamp your structure into an array of strings, instead of objects-of-objects. Commented May 29, 2015 at 14:20
  • 1
    I would go back and revisit how the initial array is created. You obviously thought you were creating a simple array but in fact you have created an array of objects, each with a property of '0'. Commented May 29, 2015 at 14:21
  • AT first @RiggsFolly , I have a data that i want to parse it [stackoverflow.com/questions/30492158/… after some coding i get this format of data ($data ), in fact i want to get array of objects to use it to get statistics if there is oter possibility please advice Commented May 29, 2015 at 14:43

3 Answers 3

1

You'll need to alter the structure of the array before calling json_encode().

I am not sure why you have an array of such a structure, you may want to look into how $data was created in this format. However, working with what you have:

$dates = array();
foreach ($data as $obj) {
    $dates[] = $obj->0;
}
print_r(json_encode($dates));

Also, anant kumar singh makes a very good point, if you are later deserializing this JSON, it will not produce the same array. So again, you should revisit how this array is serialized and deserialized and ensure they match.

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

3 Comments

AT first , I have a data that i want to parse it [stackoverflow.com/questions/30492158/… and after some coding i get this format of data ($data ), in fact i want to get array of objects to use it to get statistics if there is oter possibility please advice
She had tried to json_encode her $data, she's got something, but all she wanted is to get something else. She doesn't care of deserialization/decode, she can't get the wanted result - ["Thu Apr 30 12:25:12 +0000 2015","Wed Apr 15 21:57:05 +0000 2015"] in json format. I've downvoted because this answer answers to definitely different question.
You do realize both our answers achieve the same result. Furthermore, there are multiple comments suggesting to revisit the way the initial structure is made to avoid this problem at its root as all of this code would be unnecessary. Nonetheless, I appreciate you owning your down vote.
0

Try this function, pass your array as argument

function to_array_of_strings($data){
    $result_array = array();
        foreach($data as $key => $object){
            foreach($object as $object_key => $object_value){
               $result_array[] = $object_value;
            }
        }
    return $result_array;
}

4 Comments

with your solution the result is a string, but unfortunately I want an array of objects
You said you want this ["Thu Apr 30 12:25:12 +0000 2015","Wed Apr 15 21:57:05 +0000 2015"], so this is not an array of object, but array of strings. Tell me if I'm right and I'll change the answer to match what you like.
Checkout a new function to_array_of_strings($data), from updated answer.
Your first solution return a string like this ('["Thu Apr 30......]') not an array
0

I tried this an it works:

 $i=0;
    foreach($fin as $key1 => $value){
        foreach($value as $key2 => $value2){
            $dates[$i]= $value2 ;
            $i++;
        }
    }

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.