致命错误:未捕获错误:无法将stdClass类型的对象用作C:\xampp\htdocs\MyShoppingList\list.php:487中的数组
当我运行代码时,我得到一个错误: 致命错误:未捕获错误:无法在C:\xampp\htdocs\MyShoppingList\list.php中使用stdClass类型的对象作为数组。487堆栈跟踪:#0{main}在第487行的C:\xampp\htdocs\MyShoppingList\list.php中抛出 我不知道为什么致命错误:未捕获错误:无法将stdClass类型的对象用作C:\xampp\htdocs\MyShoppingList\list.php:487中的数组,php,json,Php,Json,当我运行代码时,我得到一个错误: 致命错误:未捕获错误:无法在C:\xampp\htdocs\MyShoppingList\list.php中使用stdClass类型的对象作为数组。487堆栈跟踪:#0{main}在第487行的C:\xampp\htdocs\MyShoppingList\list.php中抛出 我不知道为什么 function updateList() { //? Retrieve items from form POST, decode & unseriali
function updateList() {
//? Retrieve items from form POST, decode & unserialize using json. Empty array if not available.
$items = $_POST['items']
? json_decode(base64_decode($_POST ['items'], true))
: [];
//? Check if food and category was supplied.
if(filter_input(INPUT_POST, 'food') !== "" && filter_input(INPUT_POST, 'category') !== "") {
//? Add food/category as item to Shopping List
$items[] = [
"food" => filter_input(INPUT_POST, 'food'),
"category" => filter_input(INPUT_POST, 'category'),
];
}
return $items;
}
<input type="hidden" name="items" value="<?php echo base64_encode(json_encode($items)) ?>">
<div id="table">
<table width="100%">
<?php if ($items) { ?>
<?php foreach($items as $item){?>
<tr>
<td id="dairy">
<?php echo $item['category'];?>
</td>
</tr>
<form>
<tr>
<td id="data">
<?php echo'';?>
<label class="container">
<input
id="<?php echo "check${k}"; ?>"
type="checkbox"
name="<?php echo "check${k}"; ?>"
value="<?php echo $item['food']; ?>">
<span class="checkmark"></span>
</label>
</td>
</tr>
</form>
<?php } ?>
<?php } else { ?>
<tr>
<td align="center" style="padding: 20px">No items in list.</td>
</tr>
<?php } ?>
</table>
这是一个
json_decode
问题:它应该将true
作为第二个参数,以便作为数组获得结果。在代码中,此函数只使用一次,但没有第二个参数
这意味着:而不是
json_decode(base64_decode($_POST ['items'], true))
应该是:
json_decode(base64_decode($_POST ['items']), true)
或者,如果需要base64_解码的第二个参数:
json_decode(base64_decode($_POST ['items'], true), true)
json_decode(base64_decode($_POST ['items'], true), true)