Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP将JSON解析为html表?_Php_Html_Json_Html Table - Fatal编程技术网

如何使用PHP将JSON解析为html表?

如何使用PHP将JSON解析为html表?,php,html,json,html-table,Php,Html,Json,Html Table,我必须在我的网站上找到一张桌子。并且必须从中获取此表的数据 "" 我试了很多东西,但都没用 <!DOCTYPE html> <html> <head> <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head&g

我必须在我的网站上找到一张桌子。并且必须从中获取此表的数据 "" 我试了很多东西,但都没用

 <!DOCTYPE html>
  <html>
   <head>
    <script type="text/javascript" 
       src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   </head>
   <body>
    <?php
    $json=file_get_contents("http://west.basketball.nl/db/json
    /stand.pl?szn_Naam=2014-2015&cmp_ID=373");
            $data =  json_decode($json);

        if (count($data)) {
            // Open the table
            echo "<table>";

            // Cycle through the array
            foreach ($data as $stand) {

                // Output a row
                echo "<tr>";
                echo "<td>$afko</td>";
                echo "<td>$positie</td>";
                echo "</tr>";
            }

            // Close the table
            echo "</table>";
        }
    ?>
  </body>
</html>

我想,问题在于您使用的变量-
$afko
$positie
。请尝试使用以下代码-

// Cycle through the array
foreach ($data as $stand) {
    // Output a row
    echo "<tr>";
    echo "<td> . $stand['afko'] . </td>";
    echo "<td> . $stand['positie'] . </td>";
    echo "</tr>";
}
//在数组中循环
foreach($数据作为$stand){
//输出一行
回声“;
回声“$stand['afko']”;
回声“$stand['positie']”;
回声“;
}

确定从外部源获取数据时要做的第一件事是了解返回的内容

我也是

现在,您知道您处理的是一个对象,而不是标量值或数组

因此,请尝试以下代码:-

<?php
    $json=file_get_contents("http://west.basketball.nl/db/json/stand.pl?szn_Naam=2014-2015&cmp_ID=373");
    $data =  json_decode($json);

    if (count($data->stand)) {
        // Open the table
        echo "<table>";

        // Cycle through the array
        foreach ($data->stand as $idx => $stand) {

            // Output a row
            echo "<tr>";
            echo "<td>$stand->afko</td>";
            echo "<td>$stand->positie</td>";
            echo "</tr>";
        }

        // Close the table
        echo "</table>";
    }
?>

为什么不试试:

$data = json_decode($json, true);

如果需要递归方式:

public static function jsonToDebug($jsonText = '')
{
    $arr = json_decode($jsonText, true);
    $html = "";
    if ($arr && is_array($arr)) {
        $html .= self::_arrayToHtmlTableRecursive($arr);
    }
    return $html;
}

private static function _arrayToHtmlTableRecursive($arr) {
    $str = "<table><tbody>";
    foreach ($arr as $key => $val) {
        $str .= "<tr>";
        $str .= "<td>$key</td>";
        $str .= "<td>";
        if (is_array($val)) {
            if (!empty($val)) {
                $str .= self::_arrayToHtmlTableRecursive($val);
            }
        } else {
            $str .= "<strong>$val</strong>";
        }
        $str .= "</td></tr>";
    }
    $str .= "</tbody></table>";

    return $str;
}
公共静态函数jsonToDebug($jsonText='')
{
$arr=json_decode($jsonText,true);
$html=“”;
if($arr&&is_数组($arr)){
$html.=self::_arrayToHtmlTableRecursive($arr);
}
返回$html;
}
私有静态函数_arrayToHtmlTableRecursive($arr){
$str=”“;
foreach($arr作为$key=>$val){
$str=”;
$str.=“$key”;
$str=”;
if(is_数组($val)){
如果(!空($val)){
$str.=self::_arrayToHtmlTableRecursive($val);
}
}否则{
$str.=“$val”;
}
$str=”;
}
$str=”;
返回$str;
}
然后调用
echo YourClass::jsonToDebug($jsonText)


我对

的测试下面的代码可以用于动态转换整个表,而无需定义列名

 echo "<table>";
        echo "<tr>";
        foreach(array_keys($your_array[0]) as $head){
            echo "<td>$head</td>";
        }
        echo "</tr>";

        foreach($your_array as $row){
            echo "<tr>";            
            foreach($row as $col){
                echo "<td>$col</td>";
            }
            echo "</tr>";
        }
        echo "</table>";

        return $posicoes;
echo”“;
回声“;
foreach(数组\u键($your\u数组[0])作为$head){
回音“$head”;
}
回声“;
foreach($row形式的_数组){
回声“;
foreach(行作为$col){
回声“$col”;
}
回声“;
}
回声“;
返回$posicoes;

对不起,伙计们。。。忘了说,但这是在一个.php文件中写的什么不起作用?您是否在您的开发环境中将
error\u reporting
设置为
eall
并将
display\u errors
设置为
on
?您的意思是什么?。。。。我想不是。。。。对phpI来说还是个新问题。我猜问题在于,由于安全原因,url_fopen被设置为禁用。添加
错误报告(E\u ALL)到PHP脚本的开头。并确保在您的.htaccess或php.ini中将
display_errors
设置为
On
或在您请求后查看php日志文件。我不知道php.ini是什么,也不知道.htaccess是什么,我确实添加了错误报告,但仍然不起作用。控制台中有一个错误,是关于html的绘图集和它如何未声明,以及关于US-ASCCI或其他什么?哇,非常感谢,它终于起作用了:D你知道一种方法让它对位置进行排序吗?1.2.3.4.5.6.7.8.9.10.11等等,这是最好的答案,因为它是1)结构不可知的,2)代码效率(递归)的解释:第二个参数(true)告诉json_decode()返回关联数组,而不是给定数据的对象,如果您不熟悉OO编程,它可以更直观地使用。
public static function jsonToDebug($jsonText = '')
{
    $arr = json_decode($jsonText, true);
    $html = "";
    if ($arr && is_array($arr)) {
        $html .= self::_arrayToHtmlTableRecursive($arr);
    }
    return $html;
}

private static function _arrayToHtmlTableRecursive($arr) {
    $str = "<table><tbody>";
    foreach ($arr as $key => $val) {
        $str .= "<tr>";
        $str .= "<td>$key</td>";
        $str .= "<td>";
        if (is_array($val)) {
            if (!empty($val)) {
                $str .= self::_arrayToHtmlTableRecursive($val);
            }
        } else {
            $str .= "<strong>$val</strong>";
        }
        $str .= "</td></tr>";
    }
    $str .= "</tbody></table>";

    return $str;
}
 echo "<table>";
        echo "<tr>";
        foreach(array_keys($your_array[0]) as $head){
            echo "<td>$head</td>";
        }
        echo "</tr>";

        foreach($your_array as $row){
            echo "<tr>";            
            foreach($row as $col){
                echo "<td>$col</td>";
            }
            echo "</tr>";
        }
        echo "</table>";

        return $posicoes;