如何将 API 请求中的数据数组排列到 HTML SELECT 选项列表中?

How can i arrange data array from API request into HTML SELECT OPTION LIST?

我从网站 API 获得了一段 PHP 代码来检索数据,我希望将数据数组排列在 select 选项下拉菜单中。我怎样才能做到这一点?以下是网络提供的代码:

<?php
$data = array(

    'token' => '4B0D42AB9D061C0FADD724D2E908349D',
    'content' => 'report',
    'format' => 'json',
    'report_id' => '71',
    'rawOrLabel' => 'label',
    'rawOrLabelHeaders' => 'label',
    'exportCheckboxLabel' => 'false',
    'returnFormat' => 'json');
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://redcap-virtualbox/redcap/api/');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_VERBOSE, 0);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_AUTOREFERER, true);

curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&'));

$output = curl_exec($ch);

print $output;

curl_close($ch);

?>

生成的数据输出如下图:

[{"project_code":"17001"},{"project_code":"17002"},{"project_code":"17003"},{"project_code":"17004-CG"},{"project_code":"17005"},{"project_code":"16001"},{"project_code":"16002"},{"project_code":"16003"},{"project_code":"16004"},{"project_code":"16005"},{"project_code":"16006"},{"project_code":"16007"},{"project_code":"16008"},{"project_code":"16009"},{"project_code":"16010"},{"project_code":"16011"},{"project_code":"16012"},{"project_code":"16013"},{"project_code":"16014"},{"project_code":"16015"},{"project_code":"16016"},{"project_code":"17006"},{"project_code":"17008"},{"project_code":"17007"},{"project_code":"16017"},{"project_code":"16018"},{"project_code":"17009"},{"project_code":"17010"},{"project_code":"17011"},{"project_code":"17012"},{"project_code":"17013"},{"project_code":"17014"},{"project_code":"17015"},{"project_code":"17016"},{"project_code":"17017"},{"project_code":"17018"},{"project_code":"17019"},{"project_code":"17020"},{"project_code":"16019"},{"project_code":"17021"},{"project_code":"17022"},{"project_code":"16021"},{"project_code":"17023"},{"project_code":"16023"},{"project_code":"16024"},{"project_code":"16025"},{"project_code":"16026"},{"project_code":"16027"},{"project_code":"16028"},{"project_code":"16029"},{"project_code":"16030"},{"project_code":"16031"},{"project_code":"17024"},{"project_code":"16032"},{"project_code":"17025"},{"project_code":"17026-CG"},{"project_code":"17027"},{"project_code":"17028"},{"project_code":"17029"},{"project_code":"17030"},{"project_code":"17031"},{"project_code":"16033"},{"project_code":"16034"},{"project_code":"17032"},{"project_code":"17033"},{"project_code":"17034"},{"project_code":"17035"},{"project_code":"17036"},{"project_code":"17037"},{"project_code":"17038"},{"project_code":"17039"},{"project_code":"17040-CG"},{"project_code":"17041"},{"project_code":"17042"},{"project_code":"17044-CG"},{"project_code":"17045-CG"},{"project_code":"17043"},{"project_code":"17046"},{"project_code":"17047"},{"project_code":"17048"},{"project_code":"17049"},{"project_code":"17050"},{"project_code":"17051"},{"project_code":"17052"},{"project_code":"17053"},{"project_code":"17054"},{"project_code":"17055"},{"project_code":"17056"},{"project_code":"17057"},{"project_code":"17059-CG"},{"project_code":"17060"},{"project_code":"17061"},{"project_code":"17062"},{"project_code":"17063"},{"project_code":"17064"},{"project_code":"17065"},{"project_code":"17066"},{"project_code":"17067"},{"project_code":"17068"},{"project_code":"17069"},{"project_code":"17070"},{"project_code":"17071"},{"project_code":"17072"},{"project_code":"17073"},{"project_code":"17074"},{"project_code":"17075"},{"project_code":"17076"},{"project_code":"17077"},{"project_code":"17078"},{"project_code":"17079"},{"project_code":"17080"},{"project_code":"17081"},{"project_code":"17082"},{"project_code":"17083"},{"project_code":"17084"},{"project_code":"17085"},{"project_code":"17086"},{"project_code":"17087"},{"project_code":"17088"},{"project_code":"17089-CG"},{"project_code":"17090"},{"project_code":"17091"},{"project_code":"17092"},{"project_code":"17093"},{"project_code":"17094-CG"},{"project_code":"17095"},{"project_code":"17096"},{"project_code":"17097"},{"project_code":"17098"},{"project_code":"17099"},{"project_code":"17100"},{"project_code":"17101"},{"project_code":"17102"},{"project_code":"17103"},{"project_code":"17104"},{"project_code":"17105"},{"project_code":"17106"},{"project_code":"17107"},{"project_code":"18001"},{"project_code":"18002"},{"project_code":"16035"},{"project_code":"18003"},{"project_code":"18004-CG"},{"project_code":"18005"},{"project_code":"18006"},{"project_code":"18007"},{"project_code":"18008"},{"project_code":"18009"},{"project_code":"18010"},{"project_code":"18011"},{"project_code":"18012"},{"project_code":"18013"},{"project_code":"18014"},{"project_code":"18015"},{"project_code":"18016"},{"project_code":"18017"},{"project_code":"18018"},{"project_code":"18019"},{"project_code":"18020"},{"project_code":"18022"},{"project_code":"18023"},{"project_code":"18024"},{"project_code":"18025"},{"project_code":"18026"},{"project_code":"18027"},{"project_code":"18028"},{"project_code":"18029"},{"project_code":"18030"},{"project_code":"18031-CG"},{"project_code":"18032"},{"project_code":"18033"},{"project_code":"18034-CG"},{"project_code":"18035"},{"project_code":"18036"},{"project_code":"18037"},{"project_code":"18038"},{"project_code":"18039"},{"project_code":"18040"},{"project_code":"18041"}]

基本上我只需要将项目代码存储在 SELECT 选项菜单中。有人可以帮忙吗?非常感谢您的帮助。

使用下面的代码:

$json = json_decode($output, true);

$select = '';
$select .= '<select name="NAME">';
foreach($json as $val)
{
    $select .= '<option value="'.$val['project_code'].'">'.$val['project_code'].'</option>';
}
$select .= '</select>';
echo $select;

删除 print 语句。那只是吐出 JSON.

您需要解码 JSON,并在 select 元素内循环遍历它。

将打印语句替换为:

$json = json_decode( $output );

echo '<select name="my-select">';
foreach( $json as $item ){
    echo "<option value='{$item->project_code}'>Code: {$item->project_code}</option>";
}
echo '</select>';

它的作用是输出一个基本的 select,然后循环遍历决定的 API 响应,为每个选项回显一个值。

更新:

因为我不确定你是否知道所有的键是什么,我们可以将它们推入一个关联数组:

$json = json_decode( $output );

//Create an Empty Array, we'll add selects and options to this
$selects = [];

foreach( $json as $item ){
    foreach( $item as $key => $val ){
        $selects[$key][] = $val;
    }
}

//Now we have an associative array of keys (project_code, dept, etc.)
//and those have arrays associate with the appropriate key

foreach( $selects as $name => $options ){
    echo "<select name='$name'>";
        echo "<option value=''>Choose a $name</option>";
        foreach( $options as $option ){
            echo "<option value='$option'>$option</option>";
        }
    echo '</select>';
}