PHP将密钥添加到已解码然后编码的JSON数据中 [英] PHP adds keys to decoded and then encoded JSON data

查看:93
本文介绍了PHP将密钥添加到已解码然后编码的JSON数据中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定为什么会这样,但是我似乎经常遇到这个问题.这是我购物车的原始JSON:

I'm not sure why this is happening, but I seem to run into this problem often. Here is my original JSON for a shopping cart:

{
    "cartitems": [
        {
            "Product_ID": "1",
            "quantity": "1",
            "cartid": 1
        },
        {
            "Product_ID": "5",
            "quantity": "1",
            "cartid": 4
        },
        {
            "Product_ID": "5",
            "quantity": "1",
            "cartid": 6
        },
        {
            "Product_ID": "5",
            "quantity": "1",
            "cartid": 7
        }
    ]
}

此JSON数据存储到$ _SESSION变量$ _SESSION ['cart_items']

This JSON data is stored to the $_SESSION variable, $_SESSION['cart_items']

此代码用于删除项目:

$cartid = $_POST['varA'];

/* Remove the item */
foreach ($_SESSION['cart_items']['cartitems'] as $key => $product) {
    if ($product['cartid'] == $cartid) {
        unset($_SESSION['cart_items']['cartitems'][$key]);
    }
}


echo json_encode($_SESSION['cart_items']);

当Cartid = 7的项被删除时,其消灭的结果是这样的:

When the item with cartid = 7 is removed, the result is this when it is endoded:

{
    "cartitems": {
        "0": {
            "Product_ID": "1",
            "quantity": "1",
            "cartid": 1
        },
        "1": {
            "Product_ID": "5",
            "quantity": "1",
            "cartid": 4
        },
        "2": {
            "Product_ID": "5",
            "quantity": "1",
            "cartid": 6
        }
    }
}

它添加了密钥!仅当有3个以上的项目使我感到困惑时,才会发生这种情况.有什么办法可以重写代码,从而防止创建这些密钥?

It adds keys! This only occurs when there are more than 3 items, which baffles me. Is there any way I can re-write my code so that it prevents theses keys from being created?

推荐答案

在PHP中,只有数组,这些数组用于关联和数字索引的地图/列表/数组. Javascript/JSON具有两个不同的概念:数字索引数组([...])和对象映射({ foo : ... }).为了让PHP的json_encode决定在编码数组时使用哪个,幕后有一些逻辑.通常,如果数组键是连续的且全为数字,则该数组将编码为JSON数组([...]).如果甚至有一个键混乱或一个非数字键,则使用JSON对象代替.

In PHP, there are only arrays, which are used for both associative and numerically indexed maps/lists/arrays. Javascript/JSON has two distinct concepts: numerically indexed arrays ([...]) and object maps ({ foo : ... }). For PHP's json_encode to decide which to use when encoding an array, there's some logic behind the scenes. Generally, if the array keys are contiguous and all numerical, the array is encoded to a JSON array ([...]). If there's even one key out of order or a non-numeric key, a JSON object is used instead.

我不知道为什么您的数组操作特别会触发一个对象.不过,为避免这种情况,您可以重置数组键以确保它们在数字上连续索引:

Why your array manipulation in particular triggers an object, I don't know. To avoid this though, you can reset your array keys to make sure they're numerically, contiguously indexed:

$_SESSION['cart_items']['cartitems'] = array_values($_SESSION['cart_items']['cartitems']);

这篇关于PHP将密钥添加到已解码然后编码的JSON数据中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆