使用PHP的数组中的JSON输出爆炸

时间:2018-03-23 05:12:28

标签: php html json decode explode

所以,我有一个使用JSON的API输出:

{"listings":[{"adoption_fee":"$200","adoption_process":"For cats, please fill out our \u003ca href=\"http://www.haart.org.au/pre-adoption-form-cats/\"\u003ePre-Adoption Questionnaire - Cats\u003c/a\u003e.\r\n\r\nFor dogs, please fill out our \u003ca href=\"http://www.haart.org.au/pre-adoption-form-dogs/\"\u003ePre-Adoption Questionnaire - Dogs\u003c/a\u003e.\r\n\r\nFor more information on our Adoption Process, please visit this \u003ca href=\"http://www.haart.org.au/our-adoption-process/\"\u003elink\u003c/a\u003e.\r\n\r\nPlease make sure that you are familiar with our \u003ca href=\"http://www.haart.org.au/adoption-agreement/\"\u003eAdoption Agreement\u003c/a\u003e as it has recently changed.\r\n\r\nFor more information on any of our animals, please \u003ca href=\"http://www.haart.org.au/contact-us/\"\u003eContact Us\u003c/a\u003e.","age":"5 years 8 months","breeds":["Domestic Short Hair"],"breeds_display":"Domestic Short Hair","coat":"Short","contact_name":null,"contact_number":"08 6336 9410","contact_preferred_method":"Email","created_at":"2/9/2014 15:23","date_of_birth":"12/7/2012","desexed":true,"foster_needed":false,"gender":"Female","group":"Homeless and Abused Animal Rescue Team","heart_worm_treated":null,"id":316602,"interstate":false,"last_updated":"22/3/2018 9:40","medical_notes":"","microchip_number":"","mix":false,"multiple_animals":false,"name":"Whinney HC13-154","personality":"Whinney is an independent girl who likes lazing around the house, she's not bothered by other cats or dogs as long as they don't want to cuddle too much, then she will find her own alone space. \r\n\r\nShe will come up for the occasional cuddle but generally a cosy spot at the end of the bed or couch is all this beautiful girl craves :) \r\n\r\n** PLEASE NOTE: all HAART cats are to be adopted as indoor only cats for their safety and to comply with the legal requirements of the Cat Act. \r\nHAART recommends the use of Oscillot cat fencing or feline safe Catio's/portable caboodle to ensure they have access to the outdoors. \r\nPlease ask us for information on other suitable products **","photos":[{"small_80":"https://res.cloudinary.com/petrescue/image/upload/h_80,w_80,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg","medium_130":"https://res.cloudinary.com/petrescue/image/upload/h_130,w_130,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg","large_340":"https://res.cloudinary.com/petrescue/image/upload/h_340,w_340,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg","xlarge_900":"https://res.cloudinary.com/petrescue/image/upload/h_900,w_900,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg"},{"small_80":"https://res.cloudinary.com/petrescue/image/upload/h_80,w_80,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg","medium_130":"https://res.cloudinary.com/petrescue/image/upload/h_130,w_130,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg","large_340":"https://res.cloudinary.com/petrescue/image/upload/h_340,w_340,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg","xlarge_900":"https://res.cloudinary.com/petrescue/image/upload/h_900,w_900,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg"},{"small_80":"https://res.cloudinary.com/petrescue/image/upload/h_80,w_80,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg","medium_130":"https://res.cloudinary.com/petrescue/image/upload/h_130,w_130,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg","large_340":"https://res.cloudinary.com/petrescue/image/upload/h_340,w_340,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg","xlarge_900":"https://res.cloudinary.com/petrescue/image/upload/h_900,w_900,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg"}],"senior":false,"size":null,"species":"Cat","state":"WA","postcode":"6000","vaccinated":"Yes","wormed":"Yes"}

我想抓住“adoption_process”和“个性”部分,并且当输出中出现“\ n”时使用PHP断行。

我有以下内容:

$adoption_process = {
  foreach ($line in explode("\n", $json['adoption_process']))
     {$line = trim($line);
    }
}

然后我使用print_r来回显输出。但它不起作用。

对其他(更好)方法的建议持开放态度。

1 个答案:

答案 0 :(得分:-1)

您提到的JSON无效。最后它没有正确关闭阵列。我复制了JSON并进行了更改。请看下面,希望它会帮助你。

<?php 

$json = '{"listings":[{"adoption_fee":"$200","adoption_process":"For cats, please fill out our \u003ca href=\"http://www.haart.org.au/pre-adoption-form-cats/\"\u003ePre-Adoption Questionnaire - Cats\u003c/a\u003e.\r\n\r\nFor dogs, please fill out our \u003ca href=\"http://www.haart.org.au/pre-adoption-form-dogs/\"\u003ePre-Adoption Questionnaire - Dogs\u003c/a\u003e.\r\n\r\nFor more information on our Adoption Process, please visit this \u003ca href=\"http://www.haart.org.au/our-adoption-process/\"\u003elink\u003c/a\u003e.\r\n\r\nPlease make sure that you are familiar with our \u003ca href=\"http://www.haart.org.au/adoption-agreement/\"\u003eAdoption Agreement\u003c/a\u003e as it has recently changed.\r\n\r\nFor more information on any of our animals, please \u003ca href=\"http://www.haart.org.au/contact-us/\"\u003eContact Us\u003c/a\u003e.","age":"5 years 8 months","breeds":["Domestic Short Hair"],"breeds_display":"Domestic Short Hair","coat":"Short","contact_name":null,"contact_number":"08 6336 9410","contact_preferred_method":"Email","created_at":"2/9/2014 15:23","date_of_birth":"12/7/2012","desexed":true,"foster_needed":false,"gender":"Female","group":"Homeless and Abused Animal Rescue Team","heart_worm_treated":null,"id":316602,"interstate":false,"last_updated":"22/3/2018 9:40","medical_notes":"","microchip_number":"","mix":false,"multiple_animals":false,"name":"Whinney HC13-154","personality":"Whinney is an independent girl who likes lazing around the house, she\'s not bothered by other cats or dogs as long as they don\'t want to cuddle too much, then she will find her own alone space. \r\n\r\nShe will come up for the occasional cuddle but generally a cosy spot at the end of the bed or couch is all this beautiful girl craves :) \r\n\r\n** PLEASE NOTE: all HAART cats are to be adopted as indoor only cats for their safety and to comply with the legal requirements of the Cat Act. \r\nHAART recommends the use of Oscillot cat fencing or feline safe Catio\'s/portable caboodle to ensure they have access to the outdoors. \r\nPlease ask us for information on other suitable products **","photos":[{"small_80":"https://res.cloudinary.com/petrescue/image/upload/h_80,w_80,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg","medium_130":"https://res.cloudinary.com/petrescue/image/upload/h_130,w_130,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg","large_340":"https://res.cloudinary.com/petrescue/image/upload/h_340,w_340,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg","xlarge_900":"https://res.cloudinary.com/petrescue/image/upload/h_900,w_900,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg"},{"small_80":"https://res.cloudinary.com/petrescue/image/upload/h_80,w_80,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg","medium_130":"https://res.cloudinary.com/petrescue/image/upload/h_130,w_130,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg","large_340":"https://res.cloudinary.com/petrescue/image/upload/h_340,w_340,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg","xlarge_900":"https://res.cloudinary.com/petrescue/image/upload/h_900,w_900,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg"},{"small_80":"https://res.cloudinary.com/petrescue/image/upload/h_80,w_80,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg","medium_130":"https://res.cloudinary.com/petrescue/image/upload/h_130,w_130,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg","large_340":"https://res.cloudinary.com/petrescue/image/upload/h_340,w_340,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg","xlarge_900":"https://res.cloudinary.com/petrescue/image/upload/h_900,w_900,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg"}],"senior":false,"size":null,"species":"Cat","state":"WA","postcode":"6000","vaccinated":"Yes","wormed":"Yes"}]}';

$data = json_decode($json,true);

foreach($data['listings'] as $key => $value){
    $process = explode("\n",$value['adoption_process']);
    foreach($process as $k => $v){
        if(trim($v)){
            $line[] = $v;
        }
    }
}

print_r($line);