用PHP将JSON数据转换为HTML下拉列表

gmxoilav  于 2023-02-26  发布在  PHP
关注(0)|答案(3)|浏览(163)

如何使用PHP将XML/JSON数据转换为HTML select下拉列表?

JSON/XML数据:

{"CountryList":"<Countries><Country><Code>AF<\/Code><Name>Afghanistan<\/Name><\/Country><Country><Code>AL<\/Code><Name>Albania<\/Name><\/Country><Country><Code>DZ<\/Code><Name>Algeria<\/Name><\/Country><Country><Code>AS<\/Code><Name>American Samoa<\/Name><\/Country><Country><Code>AD<\/Code><Name>Andorra<\/Name><\/Country><Country><Code>AO<\/Code><Name>Angola<\/Name><\/Country><Country><Code>AI<\/Code><Name>Anguilla<\/Name><\/Country><Country><Code>AQ<\/Code><Name>Antarctica<\/Name><\/Country><Country><Code>AG<\/Code><Name>Antigua &amp; Barbuda<\/Name><\/Country><Country><Code>AR<\/Code><Name>Argentina<\/Name><\/Country><Country><Code>AM<\/Code><Name>Armenia<\/Name><\/Country><Country><Code>AW<\/Code><Name>Aruba<\/Name><\/Country><Country><Code>AU<\/Code><Name>Australia<\/Name><\/Country><Country><Code>AT<\/Code><Name>Austria<\/Name><\/Country><Country><Code>AZ<\/Code><Name>Azerbaijan<\/Name><\/Country><Country><Code>BS<\/Code><Name>Bahamas<\/Name><\/Country><Country><Code>BH<\/Code><Name>Bahrain<\/Name><\/Country><Country><Code>BD<\/Code><Name>Bangladesh<\/Name><\/Country><Country><Code>BB<\/Code><Name>Barbados<\/Name><\/Country><Country><Code>BY<\/Code><Name>Belarus (Belorussia)<\/Name><\/Country><\/Countries>","Error":{"ErrorCode":0,"ErrorMessage":""},"Status":1,"TokenId":"bdf0738c-7a47-410e-961a-52da9b5df935"}

所需的HTML输出:

<select>
    <option value="AF|Afghanistan">Afghanistan</option>
    <option value="AL|Albania">Albania</option>
    <option value="DZ|Algeria">Algeria</option>
    <option value="AS|American Samoa">American Samoa</option>
    <option value="AD|Andorra">Andorra</option>
    <option value="AO|Angola">Angola</option>
    <option value="AI|Anguilla">Anguilla</option>
    <option value="AQ|Antarctica">Antarctica</option>
    <option value="AG|Antigua & Barbuda">Antigua & Barbuda</option>
    <option value="AR|Argentina">Argentina</option>
    <option value="AM|Armenia">Armenia</option>
    <option value="AW|Aruba">Aruba</option>
    <option value="AU|Australia">Australia</option>
    <option value="AT|Austria">Austria</option>
    <option value="AZ|Azerbaijan">Azerbaijan</option>
    <option value="BS|Bahamas">Bahamas</option>
    <option value="BH|Bahrain">Bahrain</option>
    <option value="BD|Bangladesh">Bangladesh</option>
    <option value="BB|Barbados">Barbados</option>
    <option value="BY|Belarus (Belorussia)">Belarus (Belorussia)</option
</select>

解析后,我将获得如下数组

阵列

[国家/地区列表] =〉AF阿富汗AL阿尔巴尼亚DZ阿尔及利亚AS美属萨摩亚AD安道尔AO安哥拉AI安圭拉AQ南极AG安提瓜和巴布达AR阿根廷AMA亚美尼亚AWARubaAU澳大利亚AT奥地利AZ阿塞拜疆BSBahamasBH巴林BD孟加拉国BB巴巴多斯BY白俄罗斯(白俄罗斯)
[错误] =〉数组

[错误代码] =〉0
[错误消息] =〉

[状态] =〉1
[令牌ID] =〉5a 5e 32 c4 - 77 ee-4703-b 0 b1 - 4ff 275 ac 61 asw 0)

我获得的数据见以下链接https://drive.google.com/file/d/0B9VV_J4sKTatdWJGeHJkOVZzZ00/view?usp=sharing

jk9hmnmh

jk9hmnmh1#

数据不是XML,而是JSON字符串。
如果可以访问原始XML数据,则可以获得更好的结果。
顺便说一下,要解码当前数据,请使用json_decode

$data = json_decode($data);

然后,创建一个与countries模式匹配的模式,并匹配$data->CountryList中的所有匹配项

$pattern = '{([^>]+)</Code><Name>([^<]+)</Name></Country>}';
preg_match_all($pattern, $data->CountryList, $matches);

此时,可以通过foreach循环输出<option>列表:

foreach ($matches[0] as $key => $val) {
    echo sprintf('<option value="%s">%s</option>', $matches[1][$key], $matches[2][$key]);
}
    • 一个

编辑:模式语法

{([^<]+)</Code>([^<]+)</Name></Country>}
 └──┬──┘└──┬──┘└──┬──┘└─┬─────────────┘
    │      │      │     └ 2nd and 3rd tags (not captured)
    │      │      └ One or more chars except ‘<’ (2nd captured group)
    │      └ 1st tag (not captured)
    └ One or more chars except ‘<’ (1st captured group)
kiayqfof

kiayqfof2#

试试这个,可能对你有帮助。

    • 解决方案**
$json = json_decode($data);$source = new DOMDocument();
$source->loadXml($json->CountryList);
$xpath = new DOMXpath($source);
$target = new DOMDocument();
$options = $target->appendChild($target->createElement('select'));

foreach ($xpath->evaluate('/Countries/Country') as $country) {
    $countryCode = $xpath->evaluate('string(Code)', $country);
    $countryName = $xpath->evaluate('string(Name)', $country);
    $option = $options->appendChild(
        $target->createElement('option')
    );
    $option->setAttribute('value', $countryCode . "|" . $countryName);
    $option->appendChild($target->createTextNode($countryName));
}

$target->formatOutput = TRUE;
echo $target->saveXml($options);
    • 输出:**
<select>
        <option value="AF|Afghanistan">Afghanistan</option>
        <option value="AL|Albania">Albania</option>
        <option value="DZ|Algeria">Algeria</option>
        <option value="AS|American Samoa">American Samoa</option>
        <option value="AD|Andorra">Andorra</option>
        <option value="AO|Angola">Angola</option>
        <option value="AI|Anguilla">Anguilla</option>
        <option value="AQ|Antarctica">Antarctica</option>
        <option value="AG|Antigua & Barbuda">Antigua & Barbuda</option>
        <option value="AR|Argentina">Argentina</option>
        <option value="AM|Armenia">Armenia</option>
        <option value="AW|Aruba">Aruba</option>
        <option value="AU|Australia">Australia</option>
        <option value="AT|Austria">Austria</option>
        <option value="AZ|Azerbaijan">Azerbaijan</option>
        <option value="BS|Bahamas">Bahamas</option>
        <option value="BH|Bahrain">Bahrain</option>
        <option value="BD|Bangladesh">Bangladesh</option>
        <option value="BB|Barbados">Barbados</option>
        <option value="BY|Belarus (Belorussia)">Belarus (Belorussia)</option
     </select>
3qpi33ja

3qpi33ja3#

JSON以XML的形式包含了各个国家,因此必须先对JSON进行解码:

$json = json_decode($data);

然后解析XML:

$source = new DOMDocument();
$source->loadXml($json->CountryList);
$xpath = new DOMXpath($source);

可以使用DOM生成目标XML,创建目标文档并追加根节点,使用Xpath迭代国家并创建/追加选项元素。

$target = new DOMDocument();
$options = $target->appendChild($target->createElement('select'));

foreach ($xpath->evaluate('/Countries/Country') as $country) {
  $option = $options->appendChild(
    $target->createElement('option')
  );
  $option->setAttribute(
    'value', 
    $xpath->evaluate('string(Code)', $country)
  );
  $option->appendChild(
    $target->createTextNode(
      $xpath->evaluate('string(Name)', $country)
    )
  );
}

$target->formatOutput = TRUE;
echo $target->saveXml($options);

输出:

<select>
  <option value="AF">Afghanistan</option>
  <option value="AL">Albania</option>
  <option value="DZ">Algeria</option>
  <option value="AS">American Samoa</option>
  <option value="AD">Andorra</option>
  <option value="AO">Angola</option>
  <option value="AI">Anguilla</option>
  ...

相关问题