简体   繁体   中英

Unable to eval a string array

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!

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 . ');');
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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM