在PHP中编码javascript数组?

时间:2016-05-28 11:16:17

标签: javascript php json

我尝试在var reports = [ ["grup",24.5,101.6,"680 C.","680 C.",0,"N"], ["vul-Aca",4.501,-9.876,"192 C.","192 C.",0,"N"] ]; 编码javascript数组但是没有工作......我不明白问题出在哪里:

这是我的javascript代码

$file= "jsfolder/array.js"; //here there is only that js array
$file_contents = file_get_contents($file);
$json_string_array = substr($file_contents,strpos($file_contents,'['),-1);
$array_php = json_decode($json_string_array);

我尝试用这种方式编码:

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {

    self.dismissViewControllerAnimated(false, completion: { () -> Void in

    })

    if picker.cameraDevice == .Front
    {
        print("Image taken from front camera")
    }
    else if picker.cameraDevice == .Rear
    {
        print("Image taken from back camera")
    }
}

但我认为这是一个错误:(

非常感谢,对不起我的英语

2 个答案:

答案 0 :(得分:0)

你也必须从字符串的末尾删除分号。

php > var_dump(json_decode("[];"));
NULL
php > var_dump(json_decode("[]"));
array(0) {}

答案 1 :(得分:0)

PHP有扩展程序在php中运行javascript代码。

在php中安装V8JS扩展。此后,您可以做您正在尝试的事情。 从文件中获取内容。

$file= "jsfolder/array.js"; //here there is only that js array
$file_contents = file_get_contents($file);

V8JS :: executring方法然后执行以javascript代码提供的字符串。这将返回php中定义的数组,然后您可以继续php中的操作