开发者

What's json syntax for {"data":"w-file1","attr":{"rel":"file"}} in php?

开发者 https://www.devze.com 2023-03-29 08:27 出处:网络
What would be php code to get this json? 开发者_StackOverflow社区{\"data\": \"w-file1\", \"attr\": { \"rel\" : \"file\"}

What would be php code to get this json?

开发者_StackOverflow社区{  "data": "w-file1",
   "attr": { "rel" : "file"}
}

I am getting PHP Parse error: syntax error, unexpected T_DOUBLE_ARROW error for

$file = ("data" => "w-file1","attr" => ("rel" => "file"));
echo json_encode($file);


$file needs to be an array as does the attr key:

$file = array("data" => "w-file1","attr" => array("rel" => "file"));
echo json_encode($file);

Of course, you could inline it:

echo json_encode(array("data" => "w-file1","attr" => array("rel" => "file")));

Or, there is the OOP approach:

$file = new stdClass();
$file->data = 'w-file';
$file->attr = new stdClass();
$file->attr->rel = "file";
echo json_encode($file);


You need to specify array for $file and attr:

$file = array("data" => "w-file1","attr" => array("rel" => "file"));
echo json_encode($file);


$file = array(
    "data" => "w-file1",
    "attr" => array("rel" => "file")
);
echo json_encode($file);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号