html — вывод JSON в массиве с использованием PHP explode

Итак, у меня есть выход из API с использованием 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"}

Я хочу получить части «accept_process» и «personal» и использовать PHP, чтобы разбить строки, когда в выводе выводится «\ n».

У меня есть следующее:

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

Затем я использую print_r, чтобы повторить вывод. Но это не работает.

Открыты для предложений по другим (лучшим) способам сделать это.

-3

Решение

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);
-1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector