2

Given this array:

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/
        )

)

When convert to JSON using the code:

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

The output is:

[
    {
        "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/"
    }
]

This is working fine, but in the following array:

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/
        )

)

When encoded, the output is:

{
    "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/"
    }
}

What is the issue behind this encoding of same type array but different output?

6
  • what's problem exactly ? which lines are different ? Commented Oct 20, 2015 at 14:57
  • 1
    Looks like the first one is a normal array and the second is an associative array. They look the same when printed out in PHP but I bet the code that creates them is different Commented Oct 20, 2015 at 14:57
  • where does the second array come from? Commented Oct 20, 2015 at 14:57
  • 7
    The array that gives you faulty output has missing indices. It goes from 1 to 4 and skips 2 and 3. That is why the JSON parser dismisses it as a regular array and starts treating it as an associative array. Commented Oct 20, 2015 at 14:57
  • 1
    well spotted @klaar! Commented Oct 20, 2015 at 14:58

1 Answer 1

3

The array that gives you faulty output has missing indices. It goes from 1 to 4 and skips 2 and 3. That is why the JSON parser dismisses it as a regular array and starts treating it as an associative array.

This is due to array_unique preserving the keys and not re-indexing. array_values() will re-index the array for you:

echo json_encode(array_values($items));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.