【问题标题】:Convert a series of PHP variables to a JSON object through a loop通过循环将一系列 PHP 变量转换为 JSON 对象
【发布时间】:2021-03-01 12:41:15
【问题描述】:

我有一个循环遍历的数组,因为该数组是未知的。我想将循环的结果输出为 Json,但不幸的是,我总是收到一条错误消息:“SyntaxError:JSON.parse:JSON 数据第 1 行第 74 列的 JSON 数据后出现意外的非空白字符”。这是我的代码:

$testtt = array();
foreach (array_combine($links, $domain) as $link => $name) {
   $testtt['html'] = "<ul class='a'><li><a href=$link>$name</a></li></ul>";


    header('Content-Type: application/json');
    $arr = array_filter($testtt);
    $arr1 =  json_encode($arr, JSON_UNESCAPED_SLASHES);
    echo $arr1;
  }

正常的 JSON 应该是这样的。

{
    "html":[
        "<ul class='a'><li><a href= https://www.code.de>Code</a></li></ul>",
        "<ul class='a'><li><a href= https://www.stackoverflow.de>stack</a></li></ul>"
    ]
}

【问题讨论】:

  • 你能分享导致代码中断的值的转储吗?

标签: php html json loops web


【解决方案1】:

好的,我找到了解决方案。这是我的答案:

$testtt = array();
foreach (array_combine($links, $domain) as $link => $name) {
        $testtt['test1'][] = "<ul class='a'><li><a href=$link>$name</a></li></ul>";

            }
            header('Content-Type: application/json');
            $arr1 =  json_encode($testtt, JSON_UNESCAPED_SLASHES);
            echo $arr1;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    相关资源
    最近更新 更多