PHP adds keys to decoded and then encoded JSON dat

2020-01-29 21:34发布

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
        }
    ]
}

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

This code is used to remove an item:

$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']);

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

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 json
2条回答
【Aperson】
2楼-- · 2020-01-29 21:43

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']);
查看更多
家丑人穷心不美
3楼-- · 2020-01-29 21:56

Try this, worked for me. Transfer the array to a new array with auto keys:

/* Remove the item */
foreach ($_SESSION['cart_items']['cartitems'] as $key => $product) {
    if ($product['cartid'] == $cartid) {
        unset($_SESSION['cart_items']['cartitems'][$key]);
    }
}
$var=array();
foreach($_SESSION['cart_items']['cartitems'] as $key => $product) {
        $var['cart_items']['cartitems'][] = $product;
}
echo json_encode($var['cart_items']);
查看更多
登录 后发表回答