猿问

如何在 PHP 中访问并返回 foreach 的特定值

所以我有一个如下所示的请求响应


$json='[ {"item_cat":"Stationary","items":[{"item_name":"A4 Paper","qty":"2"},{"item_name":"Test Paper","qty":"6"}],"total":"2"},

{"item_cat":"Computer Accessory ","items":[{"item_name":"Power pack","qty":"2"}],"total":"1"},

{"item_cat":"Material","items":[{"item_name":"T-Shirt","qty":"3"},

{"item_name":"Cap","qty":"5"}],"total":"2"}]';

我正在尝试获取每个 item_name 和 qty,以便我可以使用它们来操作我的数据库。这是我所做的


$data = json_decode($json, true);

$len = count($data);


    for($i =0; $i< $len; $i++){

            $item_length = count($data[$i]['items']);


        for($c=0; $c < $item_length; $c++){

            foreach ($data[$i]['items'][$c] as $key => $value ) {


                $qty = '';

                if($key == "qty"){

                    $qty = $data[$i]['items'][$c];

                    }


                if($key == 'item_name'){

                    $item_name = "$value";

                    }


                    $sql= $db->query("SELECT `stock` from `inventory` WHERE `item_name` = '$item_name'");

                    while ($sql1 = $sql->fetch_assoc()) {

                            $stock = $sql1['stock'];

                  }


                    if($stock > $qty ){


                        $stock_balance = $stock - $qty;


                    $quantity = (int)$qty;


                         $db->query("UPDATE `inventory` SET `stock` =  (`stock` - '$quantity')   WHERE `item_name` = '$item_name'");

                    }else{

                        echo "<h3> This Operation Not Allowed: Stock Balance Is Less Than The Request <h3>";

                    }


                }

            }

        }

遇到非数字值,这是 $qty 的结果,因为我无法仅返回 qty 值。我尝试过其他几种方法。我真的很累了。如果有任何帮助,我将不胜感激。干杯!


慕田峪9158850
浏览 70回答 1
1回答

慕田峪4524236

让我们分解你的代码。这是json:[&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; "item_cat":"Stationary",&nbsp; &nbsp; &nbsp; "items":[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "item_name":"A4 Paper",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "qty":"2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "item_name":"Test Paper",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "qty":"6"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; "total":"2"&nbsp; &nbsp;},&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; "item_cat":"Computer Accessory ",&nbsp; &nbsp; &nbsp; "items":[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "item_name":"Power pack",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "qty":"2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; "total":"1"&nbsp; &nbsp;},&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; "item_cat":"Material",&nbsp; &nbsp; &nbsp; "items":[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "item_name":"T-Shirt",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "qty":"3"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "item_name":"Cap",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "qty":"5"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; "total":"2"&nbsp; &nbsp;}]现在没有 SQL 的数组循环(以确保它按预期工作):$data = json_decode($json, true);$len = count($data);&nbsp; &nbsp; for($i =0; $i< $len; $i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $item_length = count($data[$i]['items']);&nbsp; &nbsp; &nbsp; &nbsp; for($c=0; $c < $item_length; $c++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($data[$i]['items'][$c] as $key => $value ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $qty = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($key == "qty"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $qty = $data[$i]['items'][$c];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($key == 'item_name'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $item_name = "$value";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }这里的问题是:非人类变量名称以及不正确使用 JSON 对象。首先,让我们将变量重命名为可读的名称。例子:$data[$i]将是$catalog_entry(对象)$data[$i]['items']将是$catalog_entry_items(数组)$data[$i]['items'][$c]将是$catalog_entry_item(一件物品,一个物体)让我们用新变量更改代码:$data = json_decode($json, true);$len = count($data);for($i =0; $i< $len; $i++) {&nbsp; &nbsp; $catalog_entry = $data[$i];&nbsp; &nbsp; $catalog_entry_items = $data[$i]['items'];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for($c=0; $c < sizeof($catalog_entry_items); $c++) {&nbsp; &nbsp; &nbsp; &nbsp; $catalog_entry_item = $data[$i]['items'][$c];&nbsp; &nbsp; &nbsp; &nbsp; $qty = $catalog_entry_item['qty'];&nbsp; &nbsp; &nbsp; &nbsp; $item_name = $catalog_entry_item['item_name'];&nbsp; &nbsp; &nbsp; &nbsp; echo $item_name . ' : ' . $qty . "\n"; // <-- this is for testing&nbsp; &nbsp; }}运行此代码并查看预期结果:A4 Paper : 2Test Paper : 6Power pack : 2T-Shirt : 3Cap : 5很好,现在我们有了qty和item_name。我们来查询一下。首先看看你的代码:$sql= $db->query("SELECT `stock` from `inventory` WHERE `item_name` = '$item_name'");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ($sql1 = $sql->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $stock = $sql1['stock'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }两个奇怪的事情:1)可能的 SQL 注入,2)$stock用新值多次替换变量(如果我们有超过 1 行的项目inventory)。无论如何,如果这段代码有效(我无法检查),那么我们进入下一部分:if ($stock > $qty ) {&nbsp; &nbsp; $stock_balance = $stock - $qty;&nbsp; &nbsp; $quantity = (int)$qty;&nbsp; &nbsp; $db->query("UPDATE `inventory` SET `stock` =&nbsp; (`stock` - '$quantity')&nbsp; &nbsp;WHERE `item_name` = '$item_name'");} else {&nbsp; &nbsp; echo "<h3> This Operation Not Allowed: Stock Balance Is Less Than The Request <h3>";}首先,不必要地转换为整数,因此删除行$quantity = (int)$qty;并放入$stock_balance查询中。我们将有:if ($stock >= $qty ) {&nbsp; &nbsp; $stock_balance = $stock - $qty;&nbsp; &nbsp; $db->query("UPDATE `inventory` SET `stock` =&nbsp; $stock_balance WHERE `item_name` = '$item_name'");} else {&nbsp; &nbsp; echo "<h3> This Operation Not Allowed: Stock Balance Is Less Than The Request <h3>";}……好吧……不只是你累了,所以我现在就结束吧。询问是否有什么不正确或不理解的地方。
随时随地看视频慕课网APP
我要回答