0

How can I show artistName only in "wrapperType":"track"?

Here's the JSON example:

{  
   "resultCount":5,
   "results":[  
      {  
         "wrapperType":"collection",
         "artistName":"Liam Payne"
      },
      {  
         "wrapperType":"track",
         "artistName":"Liam Payne & French Montana"
      }
      {  
         "wrapperType":"track",
         "artistName":"Liam Payne & French Montana"
      }
      {  
         "wrapperType":"track",
         "artistName":"French Montana"
      }
      {  
         "wrapperType":"track",
         "artistName":"French Montana"
      }
   ]
}

and the php code:

but this will display all values

foreach ($obj->results as $row){
    echo $row->artistName;
}

4 Answers 4

4

Put if($row->wrapperType == "track") condition in your code.

Try below code:

foreach ($obj->results as $row){
    if($row->wrapperType == "track"){
        echo $row->artistName;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
foreach ($obj->results as $row){
    echo $row->artistName;
}

loops through all available items. You should check wrapperType:

foreach ($obj->results as $row){
  if ($row->wrapperType == 'track') {
    echo $row->artistName;
  }
}

Comments

0

first your json is not valid (missing ,) , you can check your desired output here

in loop used $data['results'] instead of $obj->results

Used if condition like if($row['wrapperType']=='track'){// if condition for what type of data you want

$data='{
    "resultCount": 5,
    "results": [{
            "wrapperType": "collection",
            "artistName": "Liam Payne"
        },
        {
            "wrapperType": "track",
            "artistName": "Liam Payne & French Montana"
        }, {
            "wrapperType": "track",
            "artistName": "Liam Payne & French Montana"
        }, {
            "wrapperType": "track",
            "artistName": "French Montana"
        }, {
            "wrapperType": "track",
            "artistName": "French Montana"
        }
    ]
}';
$data=json_decode($data, true);
print_r($data['results']);
foreach ($data['results'] as $row){
    if($row['wrapperType']=='track'){// if condition for what type of data you want
       echo "Artist Name: ".$row['artistName'];
       echo "Wrapper Type: ".$row['wrapperType'];
    }
}

1 Comment

what is the difference between $data['results'] and $obj->results?
0

You need to add a condition in the foreach loop to check if wrapperType is track

Like:

foreach ($obj->results as $row){
    if($row->wrapperType == "track"){
      echo $row->artistName;
   }
}

1 Comment

Hey, thanks for answering this question! An answer that contains only code is not very helpful without some additional detail. For example, why doe your answer work? Why not do it a different way? Including these things in your answers helps the question-asker - and others - to better understand your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.