php - 从 API 令牌获取数据到 php 表
问题描述
我有一个包含大约 1030 个 json 对象的 API,我需要通过搜索找到其中的几个,并只在下面显示与我的搜索匹配的那个,我该怎么做?谢谢你的回复
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Campaign Exclude App</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<h1>Campaigns</h1><br>
<input type="text" name="" id="">
<input type="submit">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Id</th>
<th scope="col">Campaign Name</th>
<th scope="col">CPA Goal</th>
<th scope="col">Click Amount</th>
</tr>
</thead>
</table>
<?php
$url='https://www.popads.net/api/campaign_list?key=xxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
$resultArray = json_decode($result);
echo "<pre>";
print_r($resultArray);
?>
</body>
</html>
解决方案
这是如何使用 foreach 循环 $resultArray 的简单示例,因此您可以通过名称找到任何对象:
$resultArray = json_decode($result);
$name = "type the name you looking for";
$myArray = array();
foreach ($resultArray as $val){
if ($val->name == $name){
$myArray[] = $val;
}
}
print_r($myArray); // print what you find
推荐阅读
- javascript - register.html:127 Uncaught ReferenceError: 用户名未在 register.html:127 (anonym) @ register.html:127 中定义
- c - 为什么我的程序通过直接运行而不是作为服务运行可以正常工作?Linux C
- python - 如何找到图的所有路径
- c# - 如何向 COM 公开 .NET 5.0 组件以在 VBA 中使用
- amcharts - AmChart showTooltipOn 始终处于具有缩放问题的强制定向树中
- r - 与 ggarrange 相同的 y 轴刻度
- elixir - Joken.verify(token, secret) 返回无效签名
- python - 在 pythonanywhere PYTHON DJANGO 上部署我的项目后无法读取静态文件
- mysql - MySQL查询以显示用逗号分隔的数据,并按不同列分组
- python - 使用列表在 Dataframe 中查找关键字匹配