我正在为当地商店进行在线搜索的项目。现在,搜索会返回一堆不必要的数据,例如添加项目时,consignID,数量等等(您可以看到结果here)。我确定这很简单,但我该如何过滤结果呢?谢谢! (您也可以找到API文档here)
这是我的代码:
<?php
$search = $_GET['search'];
if ($search == "") {
echo "Please enter a query. <a href='/search.php'>Click Here</a> to go back";
break;
}
else {
$data = array('key' => $API_KEY,
/*'consignorId' => '1',*/
'query' => $search,
'includeItemsWithQuantityZero' => 'false');
$data_string = json_encode($data);
$context = stream_context_create(array(
'http' => array(
'method' => "POST",
'header' => "Accept: application/json\r\n".
"Content-Type: application/json\r\n",
'content' => $data_string
)
));
$result = file_get_contents('https://user.traxia.com/app/api/inventory', false, $context);
$jsonData = $result;
$phpArray = json_decode($jsonData, true);
$phpArray = $phpArray['results'];
$activeonly = array_filter($phpArray, function($active) { return $status['status']=="ACTIVE"; });
}
?>
<html>
<table border="1" cellpadding="8" cellspacing="0" style="border-collapse: collapse">
<!-- border, cellspacing, and cellpadding attributes are not recommended; only included for example's conciseness -->
<thead>
<tr>
<?php
foreach(array_keys($phpArray[0]) as $k) {
echo "<th>$k</th>";
}
?>
</tr>
</thead>
<tbody>
<?php
foreach($phpArray as $key => $values) {
echo '<tr>';
foreach($values as $v) {
echo "<td>" . implode('<br /> ', (array)$v) . "</td>";
}
echo '</tr>';
}
?>
</tbody>
</table>
</html>
答案 0 :(得分:0)
你做不到。该文档未提及限制哪些API参数作为响应对象的一部分返回的方法。
在你的代码方面,你可以简单地忽略你不关心的字段。
我假设你有类似的代码:
foreach ($response as $item)
{
// Create a <tr> with each column being populated by $item[fieldname]
}
如果您不想consignorId
,请不要在输出中添加$item['consignorId']
。
答案 1 :(得分:0)
尝试这样的事情
<?
$mykeys = array('consignorId','category','currentPrice');
?>
<table border="1" cellpadding="8" cellspacing="0" style="border-collapse: collapse">
<!-- border, cellspacing, and cellpadding attributes are not recommended; only included for example's conciseness -->
<thead>
<tr>
<?php
foreach($mykeys as $k) {
echo "<th>$k</th>";
}
?>
</tr>
</thead>
<tbody>
<?php
foreach($phpArray as $key => $values) {
echo '<tr>';
foreach($mykeys as $k) {
echo "<td>".$values[$k]."</td>";
}
echo '</tr>';
}
?>
</tbody>
</table>