• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 698
  • Last Modified:

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

0
Brant Snow
Asked:
Brant Snow
  • 3
  • 2
1 Solution
 
sikkavarunCommented:
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.
0
 
Brant SnowAuthor Commented:
its not seeming to generate similar json
0
 
sikkavarunCommented:
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.
0
 
Brant SnowAuthor Commented:
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.
0
 
sikkavarunCommented:
You can use json_decode($json_string) to transfer it first to a stdClass Object. And you can fetch the data using $obj->{'category'} where $obj is a stdClass object.

See the example below:

 
<?php
$decode = json_decode($json);

///////////////////////////////
//Now you have to take all the
//values to a category array
///////////////////////////////
$category = array();
foreach($decode as $obj)
{
    $category = array_merge($category,array($obj->{'category'}));
}

////////////////////////
//Now you have all the
//category names in
//category array
////////////////////////
print_r($category);

//////////////////////////
//Use your above mentioned
//functions for unique
//values and array_values
//////////////////////////
$arr = array_unique($category);
$arr = array_values($arr);
json_encode($arr);

Open in new window



Is this what you were looking for?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now