Emre Akman
Emre Akman

Reputation: 1213

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!

Upvotes: 0

Views: 2628

Answers (3)

Filip Roséen
Filip Roséen

Reputation: 63902

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

Upvotes: 3

Shades88
Shades88

Reputation: 8360

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

Upvotes: 1

DaveRandom
DaveRandom

Reputation: 88697

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()

Upvotes: 2

Related Questions