Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.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数据生成2列表_Php - Fatal编程技术网

尝试使用PHP从JSON数据生成2列表

尝试使用PHP从JSON数据生成2列表,php,Php,这应该是一个相当简单的问题,但我不能为我的生活让它工作的权利 我有一个类似于这样的数组形式的JSON数据,我试图生成一个2列表,其中数组中的每2个元素占用一行 [ { "type": "Ground", "field_location": "", "field_user": "", }, { "type": "Water", "field_location": "", "fie

这应该是一个相当简单的问题,但我不能为我的生活让它工作的权利

我有一个类似于这样的数组形式的JSON数据,我试图生成一个2列表,其中数组中的每2个元素占用一行

[
    {
        "type": "Ground",
        "field_location": "",
        "field_user": "",
    },
    {
        "type": "Water",
        "field_location": "",
        "field_user": "",
    },
    {
        "type": "Sewer",
        "field_location": "",
        "field_user": "",
    },
    {
        "type": "Tap",
        "field_location": "",
        "field_user": "",
    }
]

我过度简化了代码,但是函数目前看起来如下所示

function generateHtml($jsonDoc){
    $html = "";
    $html .= "<table>";
    $count = 0;   

    foreach ($jsonDoc as $key => $value) {
        $count ++;

        if ($count % 2 == 0) 
            $html .= "<tr>";

        $html .= "<td>";
        foreach ($value as $key => $val) {
            if(!empty($val))
                $html .= $key.":".$val."<br />";
        }
        $html .= "</td>";

        if ($count % 2 == 0) 
            $html .= "</tr>";
        // $count ++;
    }
    $html .= "</table>";

    print($html);
} 
函数generateHtml($jsonDoc){
$html=“”;
$html.=”;
$count=0;
foreach($jsonDoc作为$key=>$value){
$count++;
如果($count%2==0)
$html.=”;
$html.=”;
foreach($key=>$val的值){
如果(!空($val))
$html.=$key.:“$val.”
; } $html.=”; 如果($count%2==0) $html.=”; //$count++; } $html.=”; 打印(html); }
最终的HTML输出似乎总是如下所示,我似乎无法理解缺少了什么

<table>
    <tr>
        <td>Ground</td>
    </tr>
    <tr>
        <td>Water</td>
    </tr>
    <tr>
        <td>Sewer</td>
    </tr>
    <tr>
        <td>Tap</td>
    </tr>
</table>

地面
水
下水道
水龙头
相对于

<table>
    <tr>
        <td>Ground</td>
        <td>Water</td>
    </tr>
    <tr>
        <td>Sewer</td>
        <td>Tap</td>
    </tr>
</table>
```
which is what I am looking for.

地面
水
下水道
水龙头
```
这就是我要找的。
试试这个

function generateHtml($jsonDoc){
    $html = "";
    $html .= "<table>";
    $count = 1;   

    foreach ($jsonDoc as $key => $value) {
        //$count ++;

        if ($count % 2 != 0) 
            $html .= "<tr>";

        $html .= "<td>";
        foreach ($value as $key => $val) {
            if(!empty($val))
                $html .= $key.":".$val."<br />";
        }
        $html .= "</td>";

        if ($count % 2 == 0) 
            $html .= "</tr>";
        $count ++;
    }
    $html .= "</table>";

    print($html);
} 
函数generateHtml($jsonDoc){
$html=“”;
$html.=”;
$count=1;
foreach($jsonDoc作为$key=>$value){
//$count++;
如果($count%2!=0)
$html.=”;
$html.=”;
foreach($key=>$val的值){
如果(!空($val))
$html.=$key.:“$val.”
; } $html.=”; 如果($count%2==0) $html.=”; $count++; } $html.=”; 打印(html); }
试试这个

function generateHtml($jsonDoc){
    $html = "";
    $html .= "<table>";
    $count = 1;   

    foreach ($jsonDoc as $key => $value) {
        //$count ++;

        if ($count % 2 != 0) 
            $html .= "<tr>";

        $html .= "<td>";
        foreach ($value as $key => $val) {
            if(!empty($val))
                $html .= $key.":".$val."<br />";
        }
        $html .= "</td>";

        if ($count % 2 == 0) 
            $html .= "</tr>";
        $count ++;
    }
    $html .= "</table>";

    print($html);
} 
函数generateHtml($jsonDoc){
$html=“”;
$html.=”;
$count=1;
foreach($jsonDoc作为$key=>$value){
//$count++;
如果($count%2!=0)
$html.=”;
$html.=”;
foreach($key=>$val的值){
如果(!空($val))
$html.=$key.:“$val.”
; } $html.=”; 如果($count%2==0) $html.=”; $count++; } $html.=”; 打印(html); }
实现这一点的一种简单(r)方法是使用
数组\u chunk
将原始数据拆分为数组,然后只使用两个嵌套循环生成输出

在外部循环中,您创建表行(输出
之前和
之后),在内部循环中,您为每个项目创建一个表单元格

function generateHtml($jsonDoc){
    $data = array_chunk($jsonDoc, 2); // split this into an array of arrays,
                                      // containing two of the items each
    $html = '<table>';

    foreach ($data as $row) { // loop over the first level
      $html .= '<tr>';
      foreach ($row as $cell) { // loop over the second level
        $html .= '<td>'.$cell->type.'</td>';
      }
      $html .= '</tr>';
    }
    $html .= '</table>';

    print($html);
} 
函数generateHtml($jsonDoc){
$data=array_chunk($jsonDoc,2);//将其拆分为一个数组,
//每个包含两个项目
$html='';
foreach($data as$row){//在第一级上循环
$html.='';
foreach($row as$cell){//在第二级上循环
$html.=''.$cell->type';
}
$html.='';
}
$html.='';
打印(html);
} 
实现这一点的一种简单(r)方法是使用
数组\u chunk
将原始数据拆分为数组,然后只使用两个嵌套循环生成输出

在外部循环中,您创建表行(输出
之前和
之后),在内部循环中,您为每个项目创建一个表单元格

function generateHtml($jsonDoc){
    $data = array_chunk($jsonDoc, 2); // split this into an array of arrays,
                                      // containing two of the items each
    $html = '<table>';

    foreach ($data as $row) { // loop over the first level
      $html .= '<tr>';
      foreach ($row as $cell) { // loop over the second level
        $html .= '<td>'.$cell->type.'</td>';
      }
      $html .= '</tr>';
    }
    $html .= '</table>';

    print($html);
} 
函数generateHtml($jsonDoc){
$data=array_chunk($jsonDoc,2);//将其拆分为一个数组,
//每个包含两个项目
$html='';
foreach($data as$row){//在第一级上循环
$html.='';
foreach($row as$cell){//在第二级上循环
$html.=''.$cell->type';
}
$html.='';
}
$html.='';
打印(html);
} 

有两个$key变量首先更改它的名称,这会产生歧义一个简单的(r)方法来实现这一点,就是使用
array\u chunk
将原始数据拆分为一个数组,然后使用两个嵌套的循环来生成输出。“这也消除了对循环计数器进行任何模运算的需要。”SanjitBhardwaj提出了一个很好的建议。实际代码确实是这样的。@misorude谢谢你的建议。我添加了一个答案,说明了如何做到这一点。有两个$key变量首先更改了它的名称,它会产生歧义一个简单的(r)方法来实现这一点,就是使用
array\u chunk
将原始数据拆分为数组,然后使用两个嵌套循环生成输出。“这也消除了对循环计数器进行任何模运算的需要。”SanjitBhardwaj提出了一个很好的建议。实际代码确实是这样的。@misorude谢谢你的建议。我肯定会研究这一点,因为它可能更好地符合原始代码。我添加了一个答案,说明如何可以做到这一点。非常感谢。我发现问题似乎是我的逻辑中的一个缺陷,特别是打开
的初始if语句,非常感谢。我发现问题似乎是我的逻辑中的一个缺陷,特别是打开