You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PHP将变量存入数组并查看数组中的变量

把JSON响应转为PHP数组并提取查看变量

你获取到的是JSON格式的响应,不是XML,按以下步骤处理即可将值存入数组并提取查看:

步骤1:解析JSON为PHP数组

使用json_decode()函数,第二个参数传true可将JSON转为关联数组:

$url = 'https://www.connect.inspay.in//v3/money/search_beneficiary?token='.$token.'&username='.$user_name.'&sender_number='.$number;
$json_response = file_get_contents($url);
// 将JSON字符串转为关联数组
$data_array = json_decode($json_response, true);

步骤2:从数组提取值到变量

直接通过数组键名提取所需字段:

// 提取顶层状态与消息
$status = $data_array['status'];
$message = $data_array['message'];

// 提取受益人信息(data是数组,第一个元素为受益人对象)
$beneficiary = $data_array['data'][0];
$bene_id = $beneficiary['bene_id'];
$bank_id = $beneficiary['bankid'];
$acc_no = $beneficiary['accno'];
$ifsc = $beneficiary['ifsc'];
// 其他字段可按相同方式提取

步骤3:查看变量/数组内容

用var_dump()或print_r()查看详细结构,也可直接输出变量:

// 查看整个解析后的数组结构
echo "<pre>";
print_r($data_array);
echo "</pre>";

// 查看单个变量
echo "请求状态:{$status}<br>";
echo "受益人ID:{$bene_id}<br>";
echo "银行账号:{$acc_no}<br>";

完整示例代码

$url = 'https://www.connect.inspay.in//v3/money/search_beneficiary?token='.$token.'&username='.$user_name.'&sender_number='.$number;
$json_response = file_get_contents($url);
$data_array = json_decode($json_response, true);

// 提取变量
$status = $data_array['status'];
$message = $data_array['message'];
$beneficiary = $data_array['data'][0];
$bene_id = $beneficiary['bene_id'];
$acc_no = $beneficiary['accno'];

// 输出查看
echo "请求状态:{$status}<br>";
echo "提示消息:{$message}<br>";
echo "受益人ID:{$bene_id}<br>";
echo "银行账号:{$acc_no}<br>";

// 查看完整数组结构
echo "<pre>";
print_r($data_array);
echo "</pre>";

注意:如果file_get_contents()无法获取远程内容,可检查PHP的allow_url_fopen配置是否开启,或改用cURL发起请求。

内容的提问来源于stack exchange,提问作者Kalai Infotech

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 17:54:57