0

i want to convert this json to string in php, i want it to return just the titles and descriptions.

my json output codes from my url:

 {"play-list":[{"id":"48","title":"Sound Remedy \u0026 Illenium - Spirals (feat. King Deco)","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Sound Remedy \u0026 Illenium - Spirals (feat. King Deco).mp3_calitone.com.mp3","tag":"jazz,","art":"1644246783images (15).jpg_www.calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"5800450","download":"1","time":"2017-07-14 08:15:22","public":"1","likes":"0","downloads":"0","views":"215"},{"id":"385","title":"Natalie Imbruglia - Perfectly","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Perfectly_calitone.com.mp3","tag":"Pop","art":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Perfectly_calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"0","download":"1","time":"2017-07-10 20:09:08","public":"1","likes":"0","downloads":"0","views":"177"},{"id":"384","title":"Natalie Imbruglia - Shiver","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Shiver_calitone.com.mp3","tag":"blues","art":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Shiver_calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"0","download":"1","time":"2017-07-10 20:04:08","public":"1","likes":"0","downloads":"0","views":"144"},{"id":"383","title":"Natalie Imbruglia - Torn","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Torn_calitone.com.mp3","tag":"blues","art":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Torn_calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"0","download":"1","time":"2017-07-10 19:58:38","public":"1","likes":"2","downloads":"2","views":"213"}],"success":1} 

this is my function below:

function ct_playlist_data($id) {

    $jsonData = json_decode(file_get_contents('http://www.calitunes.com/android_543ASBD/playlist.php?playlists=yes&listid=6'));
            $tracks = $jsonData->{'play-list'};
            //$success = $value->{'success'};
            echo var_dump($tracks);

}

am stuck here.

thank you in advance!

4
  • did you try the JSON.stringify() function? just like here? Commented Jul 15, 2017 at 0:49
  • Do you get an error? Commented Jul 15, 2017 at 0:52
  • Check the answer, might be helpful. Commented Jul 15, 2017 at 1:00
  • @eBourgess - emmm, JSON.stringify() is javascript, OP is asking about PHP...? Commented Jul 15, 2017 at 1:20

3 Answers 3

4

Example based on your JSON

$array = json_decode($json, true);

foreach ($array['play-list'] as $val) {
  echo $val['title'] . $val['description'] . '<br>';
}

SEE DEMO HERE

Sign up to request clarification or add additional context in comments.

1 Comment

you code gave me this error Fatal error: Uncaught Error: Cannot use object of type stdClass as array in C:\xampp\htdocs\playlist\inc\calitunes_data.php:29 Stack trace: #0 C:\xampp\htdocs\playlist\index.php(90): playlist->ct_playlist_data('6') #1 {main} thrown in C:\xampp\htdocs\playlist\inc\calitunes_data.php on line 29
0

Try this code Demo is here

<?php 

$jsonData = '{"play-list":[{"id":"48","title":"Sound Remedy \u0026 Illenium - Spirals (feat. King Deco)","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Sound Remedy \u0026 Illenium - Spirals (feat. King Deco).mp3_calitone.com.mp3","tag":"jazz,","art":"1644246783images (15).jpg_www.calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"5800450","download":"1","time":"2017-07-14 08:15:22","public":"1","likes":"0","downloads":"0","views":"215"},{"id":"385","title":"Natalie Imbruglia - Perfectly","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Perfectly_calitone.com.mp3","tag":"Pop","art":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Perfectly_calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"0","download":"1","time":"2017-07-10 20:09:08","public":"1","likes":"0","downloads":"0","views":"177"},{"id":"384","title":"Natalie Imbruglia - Shiver","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Shiver_calitone.com.mp3","tag":"blues","art":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Shiver_calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"0","download":"1","time":"2017-07-10 20:04:08","public":"1","likes":"0","downloads":"0","views":"144"},{"id":"383","title":"Natalie Imbruglia - Torn","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Torn_calitone.com.mp3","tag":"blues","art":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Torn_calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"0","download":"1","time":"2017-07-10 19:58:38","public":"1","likes":"2","downloads":"2","views":"213"}],"success":1}';
$decode = json_decode($jsonData,true);

foreach($decode['play-list'] as $d){
  echo $d['title'];
  echo $d['description'];
}

?>      

2 Comments

i need all the titles in the json
OK, simply do that using foreach. :)
0
// $json = your json    
$json = json_decode($json,true);    
$title =[];
foreach($json['play-list'] as $value) {    
    array_push($title,$value['title']);    
}

echo implode('',$title);

echo json_encode($title);

Can work?

1 Comment

add a true parameter to json_decode($json,true) can make json be a array struct.@cale_b

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.