php 多个json数组,尝试将多个数据放入php / json数组

这可能很简单..但我只是想不出该怎么做.

我有一个照片上传脚本,我想在数组中返回两种类型的数据,现在我只有一组用于返回照片的数据.

现在我这样做:

$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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值