Jump to content

Retrieving Json with duplicate values?


Go to solution Solved by Ch0cu3r,

Recommended Posts

I am trying to display Json data using PHP but I have ran into a problem.


This is the json:



},
{
"node_title": "Room",
"Room description": "This is Room 1",
"Room name": "Room 1",
"Room Type": "Bedroom",
"Room attributes": [

]
},
{
"node_title": "Room",
"Room description": "This is Room 2",
"Room name": "Room 2",
"Room Type": "Bedroom",
"Room attributes": [

This is my PHP:



<?php
$code = isset($_POST['search']) ? $_POST['search'] : '123nocode123456';
$roomjson = file_get_contents("http://my.json.com/". $code);
$roomobj = json_decode($roomjson);
?>

<?php
echo '<div role="tabpanel">
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>';

if (empty($roomobj)) {
echo '</div>';
}else{
if ($roomobj{0}->{'Room Type'} == 'Bedroom') {
echo '<li role="presentation"><a href="#bedroom1" aria-controls="bedroom1" role="tab" data-toggle="tab">';
echo ($roomobj{0}->{'Room name'});
echo '</a></li></div>';
?>

The problem is, I have two json 'nodes' that have the same 'Room Type' value, so how can I differentiate between the two? But I need to display both of them. I think I can use foreach but I need the values 'Room Name' to be in separate <li>.


Link to comment
https://forums.phpfreaks.com/topic/295955-retrieving-json-with-duplicate-values/
Share on other sites

  • Solution

Yes you would need to use a foreach loop to output all rooms returned in the json.

if (!empty($roomobj))
{
    // loop over all rooms
    foreach($roomobj as $room)
    {
        if ($room->{'Room Type'} == 'Bedroom')
        {
            echo '<li role="presentation"><a href="#bedroom1" aria-controls="bedroom1" role="tab" data-toggle="tab">';
            echo ($room->{'Room name'});
            echo '</a></li>';
        }
    }
}

echo '</div>'; // closes tabpanel div
Edited by Ch0cu3r

 

Yes you would need to use a foreach loop to output all rooms returned in the json.

if (!empty($roomobj))
{
    // loop over all rooms
    foreach($roomobj as $room)
    {
        if ($room->{'Room Type'} == 'Bedroom')
        {
            echo '<li role="presentation"><a href="#bedroom1" aria-controls="bedroom1" role="tab" data-toggle="tab">';
            echo ($room->{'Room name'});
            echo '</a></li>';
        }
    }
}

echo '</div>'; // closes tabpanel div

 

But won't that display the 'Room Names' all together? I need each Room Name to be in a separate <li> you see  :confused:

Edited by Alpha2Bravo
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.