Need help in getting some part from my json. I already search for this on website and google but didn't find good example. This my json :
{
"_doc": "lusti",
"id": 123456,
"title": "Dokumenku dari Json",
"parsel": {
"_doc": "parsel",
"id": 3256,
"doc_type": "Word",
"title": "bendahara12.doc",
"download_link": {
"Word": [
{
"label": "doc1",
"file": "http://example.com/file_doc1.doc"
},
{
"label": "doc2",
"file": "http://example.com/file_doc2.doc"
},
{
"label": "doc3",
"file": "http://example.com/file_doc3.doc"
},
{
"label": "doc4",
"file": "http://example.com/file_doc4.doc"
}
]
}
}
and this my table :
<td class="tg-baqh"><a class="btn" href="<?php echo $json['???']; ?>">Doc 1</a></td>
<td class="tg-baqh"><a class="btn" href="<?php echo $json['???']; ?>">Doc 2</a></td>
<td class="tg-baqh"><a class="btn" href="<?php echo $json['???']; ?>">Doc 3</a></td>
<td class="tg-baqh"><a class="btn" href="<?php echo $json['???']; ?>">Doc 4</a></td>
i need get link for my download button. look like :
<td class="tg-baqh"><a class="btn" href="http://example.com/file_doc1.doc">Doc 1</a></td>
<td class="tg-baqh"><a class="btn" href="http://example.com/file_doc2.doc">Doc 2</a></td>
<td class="tg-baqh"><a class="btn" href="http://example.com/file_doc3.doc">Doc 3</a></td>
<td class="tg-baqh"><a class="btn" href="http://example.com/file_doc4.doc">Doc 4</a></td>
You need to parse JSON data
using json_decode():
$array = json_decode($json_string,true);
Now you can get data in array format. Now you need to write foreach loop
for retrieve download link data.Like below:
$download_link = $array['parsel']['download_link']['Word'];
foreach($download_link as $key=>$val){
echo '<td class="tg-baqh"><a class="btn" href="' . $val['file'] . '">'. $val['label'] .'</a></td>';
}
You can use json_decode to use your json as an array in php :
$data = json_decode($json);
Then if you know your json structure will remain the same, you can directly traverse your array to loop through the download links :
foreach($data['parsel']['download_link']['Word'] as $k => $link) {
echo '<td class="tg-baqh"><a class="btn" href="' . $link['file'] . '">'. $link['label'] .'</a></td>';
}
You would need to use json_decode(); this would parse JSON data:
$array = json_decode($json_string,true);
Then you can write a foreach loop for the download link:
$download_link = $array['parsel']['download_link']['Word'];
foreach($download_link as $key=>$val){
echo '<td class="tg-baqh"><a class="btn" href="' . $val['file'] . '">'. $val['label'] .'</a></td>';
}