What is this object and how to use it?

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

There are 3 main sections and 4 parts consisting of "issuTime" "text" "url" and "validToTime", how do you start converting this into an object? If there were only 1 main section, it would be quite simple to do, however, with 3 main parts, and no identification for each main section made me scratch my head, how to start.

Any recommendations 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"
}
]
+3
source share
4 answers

Here's how to parse this 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/>";
}
?>

$obj, , foreach, , , , :

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

HTML- w :

echo '<a href="' . $obj[1]->url . '">' . $obj[1]->text . '</a>';
+2

JSON ( JavaScript Object ) - , . JavaScript , . JavaScript, , .

php- json_decode, php :

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

:

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

json_decode , . , :

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

, , :

$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
}
+10

Have you tried json_decode () ? He must correctly parse it on stdObject.

+1
source

Source: https://habr.com/ru/post/1749796/


All Articles