json编码php中的问题

时间:2015-10-20 14:53:43

标签: php arrays json

鉴于此数组:

Array
(
    [0] => Array
        (
            [item_id] => 53
            [item_name] => MRA Restaurant
            [category_name] => Restaurants
            [location_name] => Markhiya
            [item_description] => 
            [item_comments_count] => 0
            [item_likes_count] => 
            [item_image_file] => MRA Restaurant 01.jpg
            [item_image_location] => /images/item/
        )

    [1] => Array
        (
            [item_id] => 1
            [item_name] => MRA Restaurant Bakery & Sweets
            [category_name] => Restaurants
            [location_name] => Al Ghanim
            [item_description] => Great Kerala-style food and lunch buffet.
            [item_comments_count] => 2
            [item_likes_count] => 6
            [item_image_file] => MRA Restaurant Bakery & Sweets.jpg
            [item_image_location] => /images/item/
        )

    [2] => Array
        (
            [item_id] => 1486
            [item_name] => Al Amrah Palace Cafeteria
            [category_name] => Restaurants
            [location_name] => Al Doha Al Jadeeda
            [item_description] => 
            [item_comments_count] => 0
            [item_likes_count] => 
            [item_image_file] => Al Amrah Palace Cafeteria.jpg
            [item_image_location] => /images/item/
        )

    [3] => Array
        (
            [item_id] => 402
            [item_name] => Al Hamra
            [category_name] => Restaurants
            [location_name] => Musheireb
            [item_description] => 
            [item_comments_count] => 0
            [item_likes_count] => 
            [item_image_file] => Al Hamra.jpg
            [item_image_location] => /images/item/
        )

    [4] => Array
        (
            [item_id] => 2902
            [item_name] => Al Meera Fereej Bin Omran
            [category_name] => malls & hypermarkets
            [location_name] => Doha
            [item_description] => 
            [item_comments_count] => 0
            [item_likes_count] => 
            [item_image_file] => Al Meera Fereej Bin Omran.jpg
            [item_image_location] => /images/item/
        )

    [5] => Array
        (
            [item_id] => 1150
            [item_name] => Al Omraa Patisserie & Sweets
            [category_name] => Restaurants
            [location_name] => Markhiya
            [item_description] => 
            [item_comments_count] => 0
            [item_likes_count] => 
            [item_image_file] => Al Omraa Patisserie & Sweets.jpg
            [item_image_location] => /images/item/
        )

)

使用代码转换为JSON时:

$items=array_unique($items,SORT_REGULAR);
header('Content-Type: application/json');
echo json_encode($items);

输出结果为:

[
    {
        "item_id": "53",
        "item_name": "MRA Restaurant",
        "category_name": "Restaurants",
        "location_name": "Markhiya",
        "item_description": null,
        "item_comments_count": "0",
        "item_likes_count": null,
        "item_image_file": "MRA Restaurant 01.jpg",
        "item_image_location": "/images/item/"
    },
    {
        "item_id": "1",
        "item_name": "MRA Restaurant Bakery & Sweets",
        "category_name": "Restaurants",
        "location_name": "Al Ghanim",
        "item_description": "Great Kerala-style food and lunch buffet.",
        "item_comments_count": "5",
        "item_likes_count": "7",
        "item_image_file": "MRA Restaurant Bakery & Sweets.jpg",
        "item_image_location": "/images/item/"
    },
    {
        "item_id": "1486",
        "item_name": "Al Amrah Palace Cafeteria",
        "category_name": "Restaurants",
        "location_name": "Al Doha Al Jadeeda",
        "item_description": null,
        "item_comments_count": "0",
        "item_likes_count": null,
        "item_image_file": "Al Amrah Palace Cafeteria.jpg",
        "item_image_location": "/images/item/"
    },
    {
        "item_id": "402",
        "item_name": "Al Hamra",
        "category_name": "Restaurants",
        "location_name": "Musheireb",
        "item_description": null,
        "item_comments_count": "0",
        "item_likes_count": null,
        "item_image_file": "Al Hamra.jpg",
        "item_image_location": "/images/item/"
    },
    {
        "item_id": "2902",
        "item_name": "Al Meera Fereej Bin Omran",
        "category_name": "malls & hypermarkets",
        "location_name": "Doha",
        "item_description": null,
        "item_comments_count": "0",
        "item_likes_count": null,
        "item_image_file": "Al Meera Fereej Bin Omran.jpg",
        "item_image_location": "/images/item/"
    },
    {
        "item_id": "1150",
        "item_name": "Al Omraa Patisserie & Sweets",
        "category_name": "Restaurants",
        "location_name": "Markhiya",
        "item_description": null,
        "item_comments_count": "0",
        "item_likes_count": null,
        "item_image_file": "Al Omraa Patisserie & Sweets.jpg",
        "item_image_location": "/images/item/"
    }
]

这工作正常,但在以下数组中:

Array
(
    [0] => Array
        (
            [item_id] => 2
            [item_name] => Aalishan
            [category_name] => Restaurants
            [location_name] => Al Muntazah
            [item_description] => Authentic North Indian dishes at reasonable prices.
            [item_comments_count] => 0
            [item_likes_count] => 6
            [item_image_file] => aalishan-al-muntazah 03.jpg
            [item_image_location] => /images/item/
        )

    [1] => Array
        (
            [item_id] => 403
            [item_name] => Aalishan
            [category_name] => Restaurants
            [location_name] => Westbay
            [item_description] => 
            [item_comments_count] => 0
            [item_likes_count] => 2
            [item_image_file] => Abdul Khader Alhallab sweets.jpg
            [item_image_location] => /images/item/
        )

    [4] => Array
        (
            [item_id] => 844
            [item_name] => Jaal Broasted Chicken
            [category_name] => Restaurants
            [location_name] => Fereej Bin Mahmoud
            [item_description] => 
            [item_comments_count] => 0
            [item_likes_count] => 
            [item_image_file] => Jaal Broasted Chicken.jpg
            [item_image_location] => /images/item/
        )

    [5] => Array
        (
            [item_id] => 1140
            [item_name] => Jaal Cafeteria
            [category_name] => Restaurants
            [location_name] => Muaither
            [item_description] => 
            [item_comments_count] => 0
            [item_likes_count] => 
            [item_image_file] => Jaal Cafeteria.jpg
            [item_image_location] => /images/item/
        )

    [6] => Array
        (
            [item_id] => 627
            [item_name] => Jaal Restaurant
            [category_name] => Restaurants
            [location_name] => Musheireb
            [item_description] => 
            [item_comments_count] => 0
            [item_likes_count] => 
            [item_image_file] => Jaal Restaurant.jpg
            [item_image_location] => /images/item/
        )

    [7] => Array
        (
            [item_id] => 1422
            [item_name] => Jaal Restaurant
            [category_name] => Restaurants
            [location_name] => Muaither
            [item_description] => 
            [item_comments_count] => 0
            [item_likes_count] => 
            [item_image_file] => 3716_2015_05_29_10_15_50.jpg
            [item_image_location] => /images/item/
        )

)

编码时,输出为:

{
    "0": {
        "item_id": "2",
        "item_name": "Aalishan",
        "category_name": "Restaurants",
        "location_name": "Al Muntazah",
        "item_description": "Authentic North Indian dishes at reasonable prices.",
        "item_comments_count": "1",
        "item_likes_count": "6",
        "item_image_file": "aalishan-al-muntazah 03.jpg",
        "item_image_location": "/images/item/"
    },
    "1": {
        "item_id": "403",
        "item_name": "Aalishan",
        "category_name": "Restaurants",
        "location_name": "Westbay",
        "item_description": null,
        "item_comments_count": "0",
        "item_likes_count": "2",
        "item_image_file": "Abdul Khader Alhallab sweets.jpg",
        "item_image_location": "/images/item/"
    },
    "4": {
        "item_id": "3307",
        "item_name": "Faalin baby shop",
        "category_name": "brands & Fashions",
        "location_name": "Al Mirqab Al Jadeed  Doha",
        "item_description": "Exclusive kids showroom",
        "item_comments_count": "0",
        "item_likes_count": null,
        "item_image_file": "4045_2015_09_16_07_26_53.jpg",
        "item_image_location": "/images/item/"
    },
    "5": {
        "item_id": "844",
        "item_name": "Jaal Broasted Chicken",
        "category_name": "Restaurants",
        "location_name": "Fereej Bin Mahmoud",
        "item_description": null,
        "item_comments_count": "0",
        "item_likes_count": null,
        "item_image_file": "Jaal Broasted Chicken.jpg",
        "item_image_location": "/images/item/"
    },
    "6": {
        "item_id": "1140",
        "item_name": "Jaal Cafeteria",
        "category_name": "Restaurants",
        "location_name": "Muaither",
        "item_description": null,
        "item_comments_count": "0",
        "item_likes_count": null,
        "item_image_file": "Jaal Cafeteria.jpg",
        "item_image_location": "/images/item/"
    },
    "7": {
        "item_id": "627",
        "item_name": "Jaal Restaurant",
        "category_name": "Restaurants",
        "location_name": "Musheireb",
        "item_description": null,
        "item_comments_count": "0",
        "item_likes_count": null,
        "item_image_file": "Jaal Restaurant.jpg",
        "item_image_location": "/images/item/"
    },
    "8": {
        "item_id": "1422",
        "item_name": "Jaal Restaurant",
        "category_name": "Restaurants",
        "location_name": "Muaither",
        "item_description": null,
        "item_comments_count": "0",
        "item_likes_count": null,
        "item_image_file": "3716_2015_05_29_10_15_50.jpg",
        "item_image_location": "/images/item/"
    }
}

相同类型数组的编码背后有什么问题但输出不同?

1 个答案:

答案 0 :(得分:3)

为您提供错误输出的数组缺少索引。它从1到4并跳过2和3.这就是JSON解析器将其视为常规数组并开始将其视为关联数组的原因。

这是由于array_unique保留了密钥而没有重新编制索引。 array_values()将为您重新索引数组:

echo json_encode(array_values($items));