简体   繁体   English

无法评估字符串数组

[英]Unable to eval a string array

How can i change this to a JSON Array? 如何将其更改为JSON数组? im using the eval technique but this is not working. 即时通讯使用评估技术,但无法正常工作。 Im getting this kind of response from our upx server: 我从我们的upx服务器获得了这种响应:

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的调用中的"" ,并且当然将字母包装在引号中会使它成为..( 鼓声 ..)一个字符串。

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

If you want to turn it into json right away, use the below: 如果您想立即将其转换为json,请使用以下代码:

 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() 尽管如上所述,但如果要传输PHP数组,则应使用serialize()

You can try json_encode of php. 您可以尝试php的json_encode you can try 你可以试试

$json_array = json_encode($your_array);

This will give you json encoded array. 这将为您提供json编码的数组。 Then you can do json_decode on $json_array to get original contents back 然后您可以在$ json_array上执行json_decode以取回原始内容

Check if your php has JSON extension 检查您的php是否具有JSON扩展名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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