Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP parsing JSON

Posted on 2011-04-20
5
Medium Priority
?
673 Views
Last Modified: 2012-05-11
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
Comment
Question by:Brant Snow
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:sikkavarun
ID: 35435500
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
 
LVL 4

Author Comment

by:Brant Snow
ID: 35436636
its not seeming to generate similar json
0
 
LVL 1

Expert Comment

by:sikkavarun
ID: 35436746
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
 
LVL 4

Author Comment

by:Brant Snow
ID: 35436821
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
 
LVL 1

Accepted Solution

by:
sikkavarun earned 2000 total points
ID: 35437062
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question