0

How can i change this to a JSON Array? im using the eval technique but this is not working. Im getting this kind of response from our upx server:

array(
    'invoice' => array(
        'id' => '4',
        'realid' => '4',
        'address_rev' => NULL,
        'relation_data_id' => '3',
        'contact_set_rev' => '3',
        'business_data_rev' => '4',
        'private_data_rev' => NULL,
        // etc..
    )
); 

var_dump($newdata); // String
eval("\$newdata = \"$newdata\";");
var_dump($newdata); // Still stays as a string as shown above....

Any clue?

Ty Already!

1
  • 2
    What on earth outputs that? If you want to transmit a literal PHP array, serialize() it... Commented Dec 20, 2011 at 10:48

3 Answers 3

3

You'll facepalm really bad when I tell you this..

But the reason it's still a string is because you wrapped it in "" inside your call to eval, and of course wrapping letters inside of quotes will make it.. (drum roll ..) a string.

 eval ('$newdata = ' . $newdata . ';'); // this will do what you want

If you want to turn it into json right away, use the below:

 eval ('$newdata = json_encode (' . $newdata . ');');
Sign up to request clarification or add additional context in comments.

2 Comments

Omg, slap me :(, how can i not see that :O probally because im working for like 13 hours straight now! however, ty for the answers!
For people who arrived here from Google: another possible source of error when doing this is neglecting to add the semicolon to the end of the eval'ed string. Creating a temporary variable which you pass to eval (at least during development) allows you to double check the string for syntactical correctness.
2
var_dump($newdata); // String
eval("\$newdata = $newdata;");
var_dump($newdata); // Still stays as a string as shown above....
// eval("\$newdata = \"$newdata\";");
//                    ^         ^

Remove the double quotes. Your just putting it back into a string...

Although as I said above, if you want to transmit a PHP array, you should be using serialize()

Comments

1

You can try json_encode of php. you can try

$json_array = json_encode($your_array);

This will give you json encoded array. Then you can do json_decode on $json_array to get original contents back

Check if your php has JSON extension

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.