这可能很简单..但我只是想不出该怎么做.
我有一个照片上传脚本,我想在数组中返回两种类型的数据,现在我只有一组用于返回照片的数据.
现在我这样做:
$photos = array();
///Script for photo upload etc.
array_push($photos, $newPhotoToAdd)
//then when it's finished uploading each photo i do json_encode:
print json_encode($photos);
所有这些工作都很完美,但是现在我想为每张照片返回另一组数据.
我需要做类似的事情:
array_push($photos, ['photoID'] = $photoID, ['photoSource'] = $newPhotoToAdd)
解决方法:
原始问题
您可以像下面这样将数组推入而不是newPhotoToAdd:
$photos = array();
// perform upload
$photos[] = array(
'photoID' => $photoID,
'photoSource' => $newPhotoToAdd
);
print json_encode($photos);
Note: If you use array_push() to add one element to the array it’s
better to use $array[] = because in that way there is no overhead of
calling a function.
来自评论:用JavaScript打印出来
您正在处理一组对象,因此需要遍历它们:
for(var i in json) {
alert(json[i].photoID);
}
标签:arrays,json,php
来源: https://codeday.me/bug/20191101/1986159.html