循环通过json解码数组

时间:2015-02-02 05:42:08

标签: php json codeigniter

这是我从数据库中获取产品详细信息的代码。

$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循环中显示整个记录。那么错误是什么?

以上代码仅显示该数组中的第一条记录。

4 个答案:

答案 0 :(得分:2)

我想这很有效,

<?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
        )

)


演示:
http://3v4l.org/5D3qe


修改

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>";
    }
}

答案 1 :(得分:0)

使用

json_decode($product, true);

获取数组

答案 2 :(得分:0)

尝试修剪:

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之间的空白区域。剩下的一个选择是尝试更新PHP,这可能太多了。

从错误链接中尝试测试用例:

测试脚本:

<?

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);

?>

预期结果:

true * 5

实际结果:

BOOL(真) 布尔(假) 布尔(假) 布尔(真) 布尔(真)

答案 3 :(得分:0)

这是我的工作代码..

$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++;

        }
        }