如何在php laravel控制器中解析stringify数据

oknrviil  于 2023-03-19  发布在  PHP
关注(0)|答案(1)|浏览(156)

数据从HTML表单传递到laravel控制器我动态地呈现了addon checkbox

data.add_ons.map(async (item, i) => {
                            addOnHtml += `
                            <div class="d-flex align-items-center justify-content-between border-bottom pt-2">
                                <div>
                                    <input type="checkbox" id="addon${i}" class="addonCheck" name="addon[${i}]" value='${JSON.stringify(item)}' data-price="${item.price}">
                                    <label for="addon${i}" style="font-size: 18px;margin-left:5px"> ${item.name}</label>
                                </div>
                                <h5>
                                    $${item.price}
                                </h5>

                            </div>`
                        })

我在控制器中收到的请求,我想解析stringify addon数组为json数据
x一个一个一个一个x一个一个二个x
这段代码对我来说很有效,但我不认为这是使用foreach的最佳方法,还有其他方法吗?

wgx48brx

wgx48brx1#

实际上,您可以在原始的$request对象中完成json转换。

$str = '{
    "addon": [
      "{\"id\":1,\"name\":\"pepsi\",\"price\":10,\"created_at\":\"2022-12-09T18:12:35.000000Z\",\"updated_at\":\"2022-12-09T18:12:35.000000Z\",\"restaurant_id\":1,\"status\":1}",
      "{\"id\":2,\"name\":\"Extra Cheese\",\"price\":4,\"created_at\":\"2022-12-14T18:16:48.000000Z\",\"updated_at\":\"2022-12-14T18:16:48.000000Z\",\"restaurant_id\":1,\"status\":1}",
      "{\"id\":4,\"name\":\"Additional Fries\",\"price\":14,\"created_at\":\"2022-12-14T18:17:48.000000Z\",\"updated_at\":\"2022-12-14T18:17:48.000000Z\",\"restaurant_id\":1,\"status\":1}"
    ],
    "product": "1",
    "type": "product",
    "quantity": "1",
    "purchase_type": "addtocart",
    "_token": "SNkipeuJwfn58IWpFYWNRo9Fx1nGzJnwGXENaLR0"
}';
$request = json_decode($str);

foreach ( $request->addon as &$addon){
    $addon = json_decode($addon);
}
print_r($request);

结果

stdClass Object
(
    [addon] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1
                    [name] => pepsi
                    [price] => 10
                    [created_at] => 2022-12-09T18:12:35.000000Z
                    [updated_at] => 2022-12-09T18:12:35.000000Z
                    [restaurant_id] => 1
                    [status] => 1
                )

            [1] => stdClass Object
                (
                    [id] => 2
                    [name] => Extra Cheese
                    [price] => 4
                    [created_at] => 2022-12-14T18:16:48.000000Z
                    [updated_at] => 2022-12-14T18:16:48.000000Z
                    [restaurant_id] => 1
                    [status] => 1
                )

            [2] => stdClass Object
                (
                    [id] => 4
                    [name] => Additional Fries
                    [price] => 14
                    [created_at] => 2022-12-14T18:17:48.000000Z
                    [updated_at] => 2022-12-14T18:17:48.000000Z
                    [restaurant_id] => 1
                    [status] => 1
                )

        )

    [product] => 1
    [type] => product
    [quantity] => 1
    [purchase_type] => addtocart
    [_token] => SNkipeuJwfn58IWpFYWNRo9Fx1nGzJnwGXENaLR0
)

相关问题