【发布时间】:2014-11-23 23:26:42
【问题描述】:
简短描述:我查询我的PHP 后端,它从SQL 数据库返回数据行,数据以JSON 格式返回。数据包括每行图像的链接,以便能够创建占位符我还通过读取 32KB 图像数据来确定图像的宽度和高度,使用 webarto's answer here 中的“游侠”功能,然后附加将此数据发送到我生成的JSONArray。
这是我的代码:
$result = $conn->query ( $sql );
$response = array ();
if ($result->num_rows > 0) {
while ( $row = $result->fetch_assoc () ) {
/* get image url */
$url = $row ['image'];
/* get the first 32KB of image data */
$raw = ranger ( $url );
/* recreate a part of the original image */
$im = imagecreatefromstring ( $raw );
/* determine width and height of the image */
$width = imagesx ( $im );
$height = imagesy ( $im );
/* add width and height at the end of the array */
array_push ( $row, $width );
array_push ( $row, $height );
$response [] = $row;
}
$j_result = json_encode ( $response );
echo $j_result;
其中一行的结果如下:
{"lang":"de","title":"Sample Title","article":"Sample text","time":"1416070535","image":"someimage.jpg","source":"sample.com","0":606,"1":379}
我对@987654330@ 不是很熟悉,并设法使用array_push() 函数附加图像尺寸。但是,这给了我“0”、“1”等作为这些值的键,正如您在上面的 JSON 中看到的那样。
问题:如何设置这些键,使json_encode 不会自动设置它们?
【问题讨论】:
标签: php