遍历 json 解码数组
looping through json decoded array
这是我从数据库中获取产品详细信息的代码..
$i=0;
foreach($res->result() as $row ){
$products=json_decode($row->product_name,1);
//var_dump($products);
/*$sess_products[$i]['product_id'] = $row->product_id;
$sess_products[$i]['product_name'] = $row->product_name;
$sess_products[$i]['quantity'] = $row->quantity;
$sess_products[$i]['unit'] = $row->unit;
$sess_products[$i]['unit_rate'] = $row->unit_rate;
$this->session->set_userdata('sess_products',$sess_products);*/
//$post_array['cart']=$this->session->userdata('sess_products');
echo "<tr>";
echo "<td><input type='hidden' style='width:80%;' value='".$products[$i]['product_id']."' name='product_id[]'/></td>";
echo "<td><input type='hidden' style='width:80%;' value='".$products[$i]['product_name']."' name='product_name[]'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:40%;'>".$products[$i]['product_name']."</td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$products[$i]['quantity']."' name='quantity[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$products[$i]['unit']."' name='unit[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$products[$i]['unit_rate']."' name='unit_rate[]'/></td>";
echo "<td><a href='javascript:void(0)' rownum='".$i."' class='remove_from_update_cart'><img src='images/close.png'/></a></td>";
echo "</tr>";
$i++;
}
现在我可以通过解码显示 json 字符串中的第一项。但我想在 foreach 循环中显示整个记录。?那么会出现什么错误呢??
以上代码仅显示该数组中的第一条记录。
使用
json_decode($product, true);
获取为数组
尝试修剪:
foreach($res->result() as $row ){
$product=$row->product_name;
$products=json_decode(trim($product), 1);
print_r($products);
}
尝试过:
var_dump(json_decode('[{"product_id":"1","product_name":"Apple iMac","quantity":"32","unit":"23","unit_rate":"32"},{"product_id":"5","product_name":"Nokia E5","quantity":"543","unit":"543","unit_rate":"543"},{"product_id":"8","product_name":"Zinc Sulphate 500 ml","quantity":"5443","unit":"434","unit_rate":"5333"}]'));
更新:
不确定,但可能是 this 错误。我可以看到 Apple iMac
之间的白色 space。剩下的一个选择是尝试更新 PHP,这可能太多了。
尝试来自错误的测试用例 link:
测试脚本:
<?
function json_cmp($x, $y)
{
print var_dump(json_decode($x) === $y);
}
// works
json_cmp("true", true);
// fails - is actually true
json_cmp("tRue", NULL);
// fails - is actually NULL
json_cmp("true ", true);
// works
json_cmp("[true ] ", array(true));
// works, even though the non-array version fails
json_cmp("[tRue]", NULL);
?>
预期结果:
真实 * 5
实际结果:
布尔值(真)
布尔(假)
布尔(假)
布尔(真)
布尔(真)
我猜这是可行的,
<?php
$product = '[{"product_id":"1","product_name":"Apple iMac","quantity":"32","unit":"23","unit_rate":"32"},{"product_id":"5","product_name":"Nokia E5","quantity":"543","unit":"543","unit_rate":"543"},{"product_id":"8","product_name":"Zinc Sulphate 500 ml","quantity":"5443","unit":"434","unit_rate":"5333"}]';
$products = json_decode($product, true);
print_r($products);
输出:
Array
(
[0] => Array
(
[product_id] => 1
[product_name] => Apple iMac
[quantity] => 32
[unit] => 23
[unit_rate] => 32
)
[1] => Array
(
[product_id] => 5
[product_name] => Nokia E5
[quantity] => 543
[unit] => 543
[unit_rate] => 543
)
[2] => Array
(
[product_id] => 8
[product_name] => Zinc Sulphate 500 ml
[quantity] => 5443
[unit] => 434
[unit_rate] => 5333
)
)
编辑:
foreach($res->result() as $row)
{
$products = json_decode($row->product_name, true);
foreach($products as $prod)
{
echo "<tr>";
echo "<td><input type='hidden' style='width:80%;' value='".$prod['product_id']."' name='product_id[]'/></td>";
echo "<td><input type='hidden' style='width:80%;' value='".$prod['product_name']."' name='product_name[]'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:40%;'>".$prod['product_name']."</td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$prod['quantity']."' name='quantity[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$prod['unit']."' name='unit[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$prod['unit_rate']."' name='unit_rate[]'/></td>";
echo "<td><a href='javascript:void(0)' rownum='".$i."' class='remove_from_update_cart'><img src='images/close.png'/></a></td>";
echo "</tr>";
}
}
这是我的工作代码..
$i=0;
foreach($res->result() as $row ){
$j=0;
$products=json_decode($row->product_name,1);
foreach($products as $row2){
$sess_products[$j]['product_id'] = $row2['product_id'];
$sess_products[$j]['product_name'] = $row2['product_name'];
$sess_products[$j]['quantity'] = $row2['quantity'];
$sess_products[$j]['unit'] = $row2['unit'];
$sess_products[$j]['unit_rate'] = $row2['unit_rate'];
$this->session->set_userdata('sess_products',$sess_products);
//print_r($sess_products);
//$post_array['cart']=$this->session->userdata('sess_products');
echo "<tr>";
echo "<td><input type='hidden' style='width:80%;' value='".$row2['product_id']."' name='product_id[]'/></td>";
echo "<td><input type='hidden' style='width:80%;' value='".$row2['product_name']."' name='product_name[]'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:40%;'>".$row2['product_name']."</td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$row2['quantity']."' name='quantity[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$row2['unit']."' name='unit[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$row2['unit_rate']."' name='unit_rate[]'/></td>";
echo "<td><a href='javascript:void(0)' rownum='".$j."' class='remove_from_update_cart'><img src='images/close.png'/></a></td>";
echo "</tr>";
$j++;
}
}
这是我从数据库中获取产品详细信息的代码..
$i=0;
foreach($res->result() as $row ){
$products=json_decode($row->product_name,1);
//var_dump($products);
/*$sess_products[$i]['product_id'] = $row->product_id;
$sess_products[$i]['product_name'] = $row->product_name;
$sess_products[$i]['quantity'] = $row->quantity;
$sess_products[$i]['unit'] = $row->unit;
$sess_products[$i]['unit_rate'] = $row->unit_rate;
$this->session->set_userdata('sess_products',$sess_products);*/
//$post_array['cart']=$this->session->userdata('sess_products');
echo "<tr>";
echo "<td><input type='hidden' style='width:80%;' value='".$products[$i]['product_id']."' name='product_id[]'/></td>";
echo "<td><input type='hidden' style='width:80%;' value='".$products[$i]['product_name']."' name='product_name[]'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:40%;'>".$products[$i]['product_name']."</td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$products[$i]['quantity']."' name='quantity[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$products[$i]['unit']."' name='unit[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$products[$i]['unit_rate']."' name='unit_rate[]'/></td>";
echo "<td><a href='javascript:void(0)' rownum='".$i."' class='remove_from_update_cart'><img src='images/close.png'/></a></td>";
echo "</tr>";
$i++;
}
现在我可以通过解码显示 json 字符串中的第一项。但我想在 foreach 循环中显示整个记录。?那么会出现什么错误呢??
以上代码仅显示该数组中的第一条记录。
使用
json_decode($product, true);
获取为数组
尝试修剪:
foreach($res->result() as $row ){
$product=$row->product_name;
$products=json_decode(trim($product), 1);
print_r($products);
}
尝试过:
var_dump(json_decode('[{"product_id":"1","product_name":"Apple iMac","quantity":"32","unit":"23","unit_rate":"32"},{"product_id":"5","product_name":"Nokia E5","quantity":"543","unit":"543","unit_rate":"543"},{"product_id":"8","product_name":"Zinc Sulphate 500 ml","quantity":"5443","unit":"434","unit_rate":"5333"}]'));
更新:
不确定,但可能是 this 错误。我可以看到 Apple iMac
之间的白色 space。剩下的一个选择是尝试更新 PHP,这可能太多了。
尝试来自错误的测试用例 link:
测试脚本:
<?
function json_cmp($x, $y)
{
print var_dump(json_decode($x) === $y);
}
// works
json_cmp("true", true);
// fails - is actually true
json_cmp("tRue", NULL);
// fails - is actually NULL
json_cmp("true ", true);
// works
json_cmp("[true ] ", array(true));
// works, even though the non-array version fails
json_cmp("[tRue]", NULL);
?>
预期结果:
真实 * 5
实际结果:
布尔值(真) 布尔(假) 布尔(假) 布尔(真) 布尔(真)
我猜这是可行的,
<?php
$product = '[{"product_id":"1","product_name":"Apple iMac","quantity":"32","unit":"23","unit_rate":"32"},{"product_id":"5","product_name":"Nokia E5","quantity":"543","unit":"543","unit_rate":"543"},{"product_id":"8","product_name":"Zinc Sulphate 500 ml","quantity":"5443","unit":"434","unit_rate":"5333"}]';
$products = json_decode($product, true);
print_r($products);
输出:
Array
(
[0] => Array
(
[product_id] => 1
[product_name] => Apple iMac
[quantity] => 32
[unit] => 23
[unit_rate] => 32
)
[1] => Array
(
[product_id] => 5
[product_name] => Nokia E5
[quantity] => 543
[unit] => 543
[unit_rate] => 543
)
[2] => Array
(
[product_id] => 8
[product_name] => Zinc Sulphate 500 ml
[quantity] => 5443
[unit] => 434
[unit_rate] => 5333
)
)
编辑:
foreach($res->result() as $row)
{
$products = json_decode($row->product_name, true);
foreach($products as $prod)
{
echo "<tr>";
echo "<td><input type='hidden' style='width:80%;' value='".$prod['product_id']."' name='product_id[]'/></td>";
echo "<td><input type='hidden' style='width:80%;' value='".$prod['product_name']."' name='product_name[]'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:40%;'>".$prod['product_name']."</td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$prod['quantity']."' name='quantity[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$prod['unit']."' name='unit[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$prod['unit_rate']."' name='unit_rate[]'/></td>";
echo "<td><a href='javascript:void(0)' rownum='".$i."' class='remove_from_update_cart'><img src='images/close.png'/></a></td>";
echo "</tr>";
}
}
这是我的工作代码..
$i=0;
foreach($res->result() as $row ){
$j=0;
$products=json_decode($row->product_name,1);
foreach($products as $row2){
$sess_products[$j]['product_id'] = $row2['product_id'];
$sess_products[$j]['product_name'] = $row2['product_name'];
$sess_products[$j]['quantity'] = $row2['quantity'];
$sess_products[$j]['unit'] = $row2['unit'];
$sess_products[$j]['unit_rate'] = $row2['unit_rate'];
$this->session->set_userdata('sess_products',$sess_products);
//print_r($sess_products);
//$post_array['cart']=$this->session->userdata('sess_products');
echo "<tr>";
echo "<td><input type='hidden' style='width:80%;' value='".$row2['product_id']."' name='product_id[]'/></td>";
echo "<td><input type='hidden' style='width:80%;' value='".$row2['product_name']."' name='product_name[]'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:40%;'>".$row2['product_name']."</td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$row2['quantity']."' name='quantity[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$row2['unit']."' name='unit[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:100%;' value='".$row2['unit_rate']."' name='unit_rate[]'/></td>";
echo "<td><a href='javascript:void(0)' rownum='".$j."' class='remove_from_update_cart'><img src='images/close.png'/></a></td>";
echo "</tr>";
$j++;
}
}