Link to home
Start Free TrialLog in
Avatar of Brant Snow
Brant Snow

asked on

PHP parsing JSON

So i am trying to identify unique categories from a json file you can see below, i want to look through the json file and identify DISTINCT categories and then just make a new json just with the categories that exist.

I could create the json if I had the DISTINCT categories even in an array and i could sort the json based off of the categoryorder so those are not big deals, its just identifing DISTINCT categories.

So to summarize, although i have listed below the whatIWouldLIkeToSee json as the final result i can create all that and sort it if someone can just show me how to in php get the DISTINCT categories from the $json var.
$json = '[
    {
        "id": 1,
        "category": "main",
        "categoryorder": 2 
    },
    {
        "id": 2,
        "category": "main",
        "categoryorder": 2 
    },
    {
        "id": 3,
        "category": "text",
        "categoryorder": 1 
    },
    {
        "id": 4,
        "category": "main",
        "categoryorder": 1 
    },
    {
        "id": 5,
        "category": "image",
        "categoryorder": 3 
    }
]';

$decode = json_decode($json);

$whatIwantTheResultToBe = '
[
    {
        "category": "text",
        "categoryorder": 1 
    },
    {
        "category": "main",
        "categoryorder": 2 
    },
    {
        "category": "image",
        "categoryorder": 3 
    }
]
';

Open in new window

Avatar of sikkavarun
sikkavarun

Hi,

With your context above, you can use the following to generate a similar json with distinct categories:

$decode = json_decode($json);

$category = array();
$id = 0;
foreach($decode as $obj)
{
	if($category[$obj->{'categoryorder'}] == "")
	{
		$category[$obj->{'categoryorder'}] = array($obj->{'category'},$obj->{'categoryorder'});
	}
}
ksort($category);
echo json_encode($category);

Open in new window


Hope this helps.
Avatar of Brant Snow

ASKER

its not seeming to generate similar json
Can you post your output?
Or you can compare what i get...

 
{"1":["text",1],"2":["main",2],"3":["image",3]}

Open in new window



If you are pointing towards the "1":  "2":  "3" before the category name and order, then you need not worry about it as it is only the index number of the json object, just like an associative array in PHP. And a well structured JSON is always written with a index key. http://json.org/example.html

If you strictly need the output that you had, say for comparing with something else, then we might have to change the code a little bit. Use a couple of string comparison or string explode functions of PhP and extract the other part of the notation, and combine them to form the json similar to one you need.
Ya i got the same output but i do need to change it for comparison however if i could just get an array created from the first json say

var_dump($category); // has main, main, text,main,image

then i could do

$arr = array_unique($category);

$arr = array_values($arr);

echo (json_encode($arr));
main, text, image

then i would be good but how do i get all the values into the $category array, does that make sense?  How can i take my json and create an array with with all the "category" items in it then i could trim it down.
ASKER CERTIFIED SOLUTION
Avatar of sikkavarun
sikkavarun

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial