views:

114

answers:

4

What would be the correct name for this type of array?

There are 3 main sections and 4 sub-parts consisting of "issuedTime" "text" "url" and "validToTime", how do you start to convert this to an object? If there was only 1 main section, it would be fairly simple to do however with 3 main parts and no identification for each main section has me scratching my head as where to start.

Any advise appreciated.

[{
"issuedTime":"7:13pm Sunday 13 June 2010",
"text":"\nAmended 7:10pm.\n\nText text and more text\n",
"url":"\/folder\/fc\/name.png",
"validToTime":"12:00am Monday 14 June 2010"
},{
"issuedTime":"8:33pm Sunday 13 June 2010",
"text":"\nText and more text.\n",
"url":"\/folder\/fc\/name.png",
"validToTime":"12:00pm Monday 14 June 2010"
},{
"issuedTime":"10:40am Sunday 13 June 2010",
"text":"\nAnd even more text.",
"url":"\/folder\/fc\/name.png",
"validToTime":"12:00am Tuesday 15 June 2010"
}
]
+10  A: 

JSON (an acronym for JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript programming language for representing simple data structures and associative arrays, called objects. Despite its relationship to JavaScript, it is language-independent, with parsers available for virtually every programming language.

You can easily convert it into php array with json_decode function, here is an example from php site:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json, true));

Result:

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

The second parameter to json_decode is whether or not it should be converted into associative array. If you don't specify the second parameter, the output will be like:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

So, you can convert it to array and loop through it like:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$array = json_decode($json, true);
print_r($array);

foreach($array as $key => $value)
{
   // manipulate the var $value
}
Sarfraz
Ok, yes have tried a few json decode scripts but end up with gobertygook outputs and errors or not decoding fully to a workable text.
Gary
@Gary: You should post your attempt/code here or another question so that we could answer what is wrong there.
Sarfraz
Sorry, its not so much the decode but utilising each part of each section, if I go echo $out->text; for example, it doesn't differentiate between the sections.Sorry am I just a beginner.
Gary
$json = '{"a":issuedTime,"b":issuedTime,"c":issuedTime}';var_dump(json_decode($json, true));Will that give me the 3 separate issueTimes ?
Gary
@Gary: Yes because there are three of them there.
Sarfraz
@Gary... It's a little tricky, basically you have to use foreach to go through the created objects to retrieve the ->text or whatever.. that's why the code would help to see
Peter Ajtai
+1  A: 

Have you tried json_decode()? It should parse this correctly to a stdObject.

Nort
+2  A: 

Here's how to parse that json

<?php
$json = '[{
    "issuedTime":"7:13pm Sunday 13 June 2010",
    "text":"\nAmended 7:10pm.\n\nText text and more text\n",
    "url":"\/folder\/fc\/name.png",
    "validToTime":"12:00am Monday 14 June 2010"
    },{
    "issuedTime":"8:33pm Sunday 13 June 2010",
    "text":"\nText and more text.\n",
    "url":"\/folder\/fc\/name.png",
    "validToTime":"12:00pm Monday 14 June 2010"
    },{
    "issuedTime":"10:40am Sunday 13 June 2010",
    "text":"\nAnd even more text.",
    "url":"\/folder\/fc\/name.png",
    "validToTime":"12:00am Tuesday 15 June 2010"
}]';
// Parse the json into a PHP array that holds multiple "stdClass Object"s
$obj = json_decode($json);  
// Iterate through each "stdClass Object" and show what it contains  
foreach($obj as $var => $value)
{
    echo "Number: $var <br/>";    
    echo "Issued: " . $obj[$var]->issuedTime . "<br/>";                    
    echo "Text: " . $obj[$var]->text . "<br/>";    
    echo "URL: " . $obj[$var]->url . "<br/>";    
    echo "Valid to: " . $obj[$var]->validToTime . "<br/>";       
    echo "<br/>";
}
?>

Once you have $obj, you can access it like in the foreach loop above, or to access, let's say the text in the second listing you would use:

echo $obj[1]->text; // Second listing, since the first listing is $obj[0]

And HTML link w text for the same:

echo '<a href="' . $obj[1]->url . '">' . $obj[1]->text . '</a>';
Peter Ajtai
I will have a go with this and see what results it generates, if I could give each section an identifier such as [0], [1], [2] (being 3 sections) then could I use something simple like echo $out->[0]text; or echo $out->[2]url; could that work?
Gary
Each section already has an identifier... $var. This is made automatically for you when you decode the json. In your example (the code above) $var goes from 0 to 2. It is accessed like you say, $obj[$var]->text... echo $obj[1]->text; etc... I'm not sure I understand your comment exactly....
Peter Ajtai
@Peter, sorry to take so long to respond, What I need to do is grab each heading in each section so that I can output it to a webpage table, if it only had 1 section instead of 3, it would be fairly easy to do but unsure how to make script to grab say the url from section one and place it on the page and then grab the url from section 2 and output it somewhere else, in other words have separate identities for the 3 "issuedTime"s and 3 "texts and so forth.
Gary
Now the output is as follows... Number: 0 Issued: 7:13pm Sunday 13 June 2010Text: Amended 7:10pm. Text text and more textURL: /folder/fc/name.pngValid to: 12:00am Monday 14 June 2010Number: 1 Issued: 8:33pm Sunday 13 June 2010Text: Text and more text.URL: /folder/fc/name.pngValid to: 12:00pm Monday 14 June 2010Number: 2 Issued: 10:40am Sunday 13 June 2010Text: And even more text.URL: /folder/fc/name.pngValid to: 12:00am Tuesday 15 June 2010 so to get #2 url I just go echo $obj[2]"url"->text;
Gary
@Gary - I appended how to get the text from the second listing. Remember that we start at 0, not 1.
Peter Ajtai
Many thanks Peter, you have been a big help.
Gary