【问题标题】:looping through json decoded array循环通过json解码数组
【发布时间】:2015-04-01 01:04:01
【问题描述】:

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

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

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

【问题讨论】:

  • 为什么你在foreach 循环内做$products=json_decode($product);?您是否在循环之外尝试过?
  • 如果你想要数组,为什么不使用$res-&gt;result_array();Documentation here
  • $row 代表 JSON 还是 $row-&gt;product_name$row-&gt;product_name 看起来更像是名称的字符串列,而不是整个产品详细信息。
  • 给我们$product的确切var_dump。
  • 问题好像是code ignitor,你用的是哪个版本的?

标签: php json codeigniter


【解决方案1】:

我想这是可行的,

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

【讨论】:

  • 根据您更新的问题,检查答案中的编辑。您将需要另一个 foreach 循环。
  • 酷。请注意关闭答案以供其他人使用。 :D
  • 好的,你也需要一个标志值。我不知道。你也可以接受你的答案:)
【解决方案2】:

使用

json_decode($product, true);

以数组形式获取

【讨论】:

  • 你的 PHP 版本是多少?
【解决方案3】:

尝试修剪:

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

?>

预期结果:

真 * 5

实际结果:

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

【讨论】:

  • 我手动尝试解码您在问题中提到的字符串,这是有效的。所以只是猜测。
  • 试试var_dump,print_r 是特定于数组的。
  • 对不起,我尝试了您的手动代码,但它再次显示 json 字符串而不是数组或对象..
  • 奇怪,确认phpinfo();中是否启用了扩展,只需在php标签中运行此代码即可。并寻找json。你在 ubuntu 上吗?
【解决方案4】:

这是我的工作代码..

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

        }
        }

【讨论】:

    猜你喜欢
    • 2017-03-13
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 2019-07-20
    • 2019-10-20
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    相关资源
    最近更新 更多