简体   繁体   English

在迭代json数组上为foreach()提供了无效的参数

[英]Invalid argument supplied for foreach() on iterating json array

I have this json: 我有这个json:

[{"nombre":"Example1","idcomplejo":"1","canchafk":"1","cantidadturnos":"35"},
{"nombre":"Example2","idcomplejo":"3","canchafk":"6","cantidadturnos":"29"},
{"nombre":"Example3","idcomplejo":"4","canchafk":"7","cantidadturnos":"6"},
{"nombre":"Example4","idcomplejo":"5","canchafk":"8","cantidadturnos":"4"},
{"nombre":"Example5","idcomplejo":"5","canchafk":"9","cantidadturnos":"2"}]

I wanna show in my table into my HTML page, but I have this error: 我想在表格中显示我的HTML页面,但是出现此错误:

Invalid argument supplied for foreach() 为foreach()提供了无效的参数

This is the part of code where I getting the error: 这是我收到错误的代码部分:

<table cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <th>ID Complejo></th>
            <th># Cancha></th>
            <th>Cantidad Turnos></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($cantidadturnos as $turno): ?>
        <tr>

            <td><?= h($turno->idComplejo) ?></td>
            <td><?= h($turno->canchaFK) ?></td>
            <td><?= h($turno->cantidadTurnos) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

the var_dump of cantidadturnos is the json that i put above. cantidadturnos的var_dump是我放在上面的json。 Thanks for help 感谢帮助

The json is in the form of a JavaScript object. json采用JavaScript对象的形式。 PHP cannot read it so it needs to parse the json with json_decode() , like this: PHP无法读取它,因此需要使用json_decode()解析json,如下所示:

$cantidadturnos = json_decode('[{"nombre":"Example1","idcomplejo":"1","canchafk":"1","cantidadturnos":"35"},
{"nombre":"Example2","idcomplejo":"3","canchafk":"6","cantidadturnos":"29"},
{"nombre":"Example3","idcomplejo":"4","canchafk":"7","cantidadturnos":"6"},
{"nombre":"Example4","idcomplejo":"5","canchafk":"8","cantidadturnos":"4"},
{"nombre":"Example5","idcomplejo":"5","canchafk":"9","cantidadturnos":"2"}]');

Now $cantidadturnos is in the form of an array the foreach can work with like this: 现在$cantidadturnos是数组, foreach可以像这样使用它:

foreach ($cantidadturnos as $turno) {
    // do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM