0

Tengo la información Json de la siguiente forma en MySQL

{"adult":false,"backdrop_path":"/m67smI1IIMmYzCl9axvKNULVKLr.jpg",
        "belongs_to_collection":{"id":10194,"name":"Toy Story Collection","poster_path":"/7G9915LfUQ2lVfwMEEhDsn3kT4B.jpg","backdrop_path":"/9FBwqcd9IRruEDUrTdcaafOMKUq.jpg"},
        "budget":175000000,
        "genres":[{"id":12,"name":"Adventure"},{"id":16,"name":"Animation"},{"id":35,"name":"Comedy"},{"id":10751,"name":"Family"},{"id":14,"name":"Fantasy"}],
        "homepage":"https://www.pixar.com/feature-films-toy-story-4",
        "id":301528,
        "imdb_id":"tt1979376",
        "original_language":"en",
        "original_title":"Toy Story 4",
        "overview":"Woody has always been confident about his place in the world and that his priority is taking care of his kid, whether that's Andy or Bonnie. But when Bonnie adds a reluctant new toy called \"Forky\" to her room, a road trip adventure alongside old and new friends will show Woody how big the world can be for a toy.",
        "popularity":125.276,
        "poster_path":"/w9kR8qbmQ01HwnvK4alvnQ2ca0L.jpg",
        "production_companies":[{"id":2,"logo_path":"/wdrCwmRnLFJhEoH8GSfymY85KHT.png","name":"Walt Disney Pictures","origin_country":"US"},{"id":3,"logo_path":"/1TjvGVDMYsj6JBxOAkUHpPEwLf7.png","name":"Pixar","origin_country":"US"}],"production_countries":[{"iso_3166_1":"US","name":"United States of America"}],
        "release_date":"2019-06-19",
        "revenue":519802847,
        "runtime":100,
        "spoken_languages":[{"iso_639_1":"en","name":"English"}],
        "status":"Released",
        "tagline":"Get Ready to Hit the Road",
        "title":"Toy Story 4",
        "video":false,
        "vote_average":7.6,
        "vote_count":2580,
        "credits":{"cast":[{"cast_id":6,"character":"Woody (voice)","credit_id":"577a78b8c3a368770400058f","gender":2,"id":31,"name":"Tom Hanks","order":0,"profile_path":"/xxPMucou2wRDxLrud8i2D4dsywh.jpg"},{"cast_id":66,"character":"Buzz Lightyear (voice)","credit_id":"5c6d12ad92514129a2035517","gender":2,"id":12898,"name":"Tim Allen","order":1,"profile_path":"/PGLz0YLg4eB49BA6QxzHF5czxX.jpg"},{"cast_id":58,"character":"Bo Peep (voice)","credit_id":"5c6d12aa9251417df40bff9f","gender":1,"id":8873,"name":"Annie Potts","order":2,"profile_path":"/eryXT84RL41jHSJcMy4kS3u9y6w.jpg"},{"cast_id":63,"character":"Jessie (voice)","credit_id":"5c6d12ac9251412fc40f59a2","gender":1,"id":3234,"name":"Joan Cusack","order":3,"profile_path":"/3jcrXcFYoSKEUvokzqrQ2UJGtw.jpg"},{"cast_id":71,"character":"Forky (voice)","credit_id":"5c6d12aec3a3683bf0df729a","gender":2,"id":25147,"name":"Tony Hale","order":4,"profile_path":"/ar4uapp4w5wMkThZcqWUNMSTO8z.jpg"},{"cast_id":55,"character":"Duke Caboom (voice)","credit_id":"5c6d12aa9251412fc40f599b","gender":2,"id":6384,"name":"Keanu Reeves","order":5,"profile_path":"/bOlYWhVuOiU6azC4Bw6zlXZ5QTC.jpg"},{"cast_id":60,"character":"Ducky (voice)","credit_id":"5c6d12ab9251412fc40f599f","gender":2,"id":298410,"name":"Keegan-Michael Key","order":6,"profile_path":"/gqPRozx8ppAg2TSjW3ww56LnIZr.jpg"},{"cast_id":57,"character":"Bunny (voice)","credit_id":"5c6d12aa9251417aa20c2425","gender":2,"id":291263,"name":"Jordan Peele","order":7,"profile_path":"/9rOEfvKUInuUpJ6MMhiC8Tch1WM.jpg"},{"cast_id":92,"character":"Gabby Gabby (voice)","credit_id":"5c918de10e0a2612f55c3d1c","gender":1,"id":110014,"name":"Christina Hendricks","order":8,"profile_path":"/fqQWrKDhOTAPjjNTHSU4tBfq5NJ.jpg"},{"cast_id":93,"character":"Giggle McDimples (voice)","credit_id":"5c918dff0e0a2612ef5c5d78","gender":1,"id":209596,"name":"Ally Maki","order":9,"profile_path":"/fZK27EmQkDTtUayLX38XhLBZE9h.jpg"},{"cast_id":94,"character":"Mr. Potato Head (voice)","credit_id":"5c9cf99792514124c5312c59","gender":2,"id":7167,"name":"Don Rickles","order":10,"profile_path":"/h5BcaDMPRVLHLDzbQavec4xfSdt.jpg"},{"cast_id":73,"character":"Slinky Dog (voice)","credit_id":"5c6d12af92514129d503c75f","gender":2,"id":21485,"name":"Blake Clark","order":11,"profile_path":"/j6nhwuLKJXzHuD4zv2d3y99PcIC.jpg"},{"cast_id":62,"character":"Rex (voice)","credit_id":"5c6d12ac0e0a262c999fb6d5","gender":2,"id":12900,"name":"Wallace Shawn","order":12,"profile_path":"/oGE6JqPP2xH4tNORKNqxbNPYi7u.jpg"},{"cast_id":68,"character":"Hamm (voice)","credit_id":"5c6d12ad0e0a260e9e9b90e3","gender":2,"id":7907,"name":"John Ratzenberger","order":13,"profile_path":"/oRtDEOuIO1yDhTz5dORBdxXuLMO.jpg"},{"cast_id":72,"character":"Mrs. Potato Head (voice)","credit_id":"5c6d12af0e0a261aaf9f52f9","gender":1,"id":61964,"name":"Estelle Harris","order":14,"profile_path":"/equDBtZgBkO7e8QKpNokDz9EbY9.jpg"},{"cast_id":75,"character":"Squeeze Toy Aliens (voice)","credit_id":"5c6d12afc3a3685cbddee043","gender":2,"id":7882,"name":"Jeff Pidgeon","order":15,"profile_path":"/yLddkg5HcgbJg00cS13GVBnP0HY.jpg"},{"cast_id":59,"character":"Mr. Pricklepants (voice)","credit_id":"5c6d12ab92514129a2035514","gender":2,"id":10669,"name":"Timothy Dalton","order":16,"profile_path":"/n7QJKtA7jTwGHVuL5cPkqzu7CDG.jpg"},{"cast_id":61,"character":"Trixie (voice)","credit_id":"5c6d12ab0e0a266dacabaf13","gender":1,"id":109869,"name":"Kristen Schaal","order":17,"profile_path":"/rn84XtsczLSvJOWWNXLocaLx2df.jpg"},{"cast_id":70,"character":"Buttercup (voice)","credit_id":"5c6d12aec3a3684f97eb1407","gender":2,"id":60074,"name":"Jeff Garlin","order":18,"profile_path":"/cBs7u7rBTvDFp5MFCnvrPeF7SI2.jpg"},{"cast_id":65,"character":"Dolly (voice)","credit_id":"5c6d12ac0e0a260e9e9b90dc","gender":1,"id":5149,"name":"Bonnie Hunt","order":19,"profile_path":"/uKAfrFRZYnHFAxw53Jldbs8yIZO.jpg"},{"cast_id":74,"character":"Chuckles the Clown (voice)","credit_id":"5c6d12af0e0a261aaf9f52fc","gender":2,"id":7918,"name":"Bud Luckey","order":20,"profile_path":"/pcCh7G19FKMNijmPQg1PMH1btic.jpg"},{"cast_id":103,"character":"Bonnie (voice)","credit_id":"5d066a8092514104c4bba1c0","gender":1,"id":1530558,"name":"Madeleine McGraw","order":21,"profile_path":"/wrnJbVuxcEzmsU5FukVnFmS54Xo.jpg"},{"cast_id":96,"character":"Bonnie’s Dad (voice)","credit_id":"5d06691a92514163d1bfb253","gender":2,"id":19487,"name":"Jay Hernandez","order":22,"profile_path":"/phKVHS0PEAoWxqiYDJKL2K82ZX0.jpg"},{"cast_id":106,"character":"Andy Davis (voice)","credit_id":"5d0a59a30e0a2628abccec25","gender":2,"id":1116442,"name":"John Morris","order":23,"profile_path":"/vYGyvK4LzeaUCoNSHtsuqJUY15M.jpg"},{"cast_id":64,"character":"Mrs. Davis (voice)","credit_id":"5c6d12acc3a3685cbddee040","gender":1,"id":12133,"name":"Laurie Metcalf","order":24,"profile_path":"/oHhTbuv9eNJV4YlgizhCkZsPV0N.jpg"},{"cast_id":56,"character":"Harmony’s Mom (voice)","credit_id":"5c6d12aac3a368745eeca074","gender":1,"id":4687,"name":"Patricia Arquette","order":25,"profile_path":"/2ZIZ2xIdWKZNKRcdIe60AH25c82.jpg"},{"cast_id":69,"character":"Julia Anderson (voice)","credit_id":"5c6d12ae0e0a261aaf9f52f6","gender":1,"id":24358,"name":"Lori Alan","order":26,"profile_path":"/mNfJWzuaKgkIaK7CuirXOMosd2h.jpg"},{"cast_id":95,"character":"Axel the Carnie (voice)","credit_id":"5d0668939251413f2dbdb061","gender":2,"id":19278,"name":"Bill Hader","order":27,"profile_path":"/3Z5JsyLw7YTLZgpA3FAlLmFSN9F.jpg"},{"cast_id":97,"character":"Melephant Brooks (voice)","credit_id":"5d06694792514163d1bfb2bc","gender":2,"id":14639,"name":"Mel Brooks","order":28,"profile_path":"/ndFo3LOYNCUghQTK833N1Wtuynr.jpg"},{"cast_id":98,"character":"Bitey White (voice)","credit_id":"5d0669640e0a260b0ad139af","gender":1,"id":71727,"name":"Betty White","order":29,"profile_path":"/mgajnHMGottmiuwOeIRicx9BzbD.jpg"},{"cast_id":99,"character":"Combat Carl (voice)","credit_id":"5d0669a80e0a260b0ad139de","gender":2,"id":1101,"name":"Carl Weathers","order":30,"profile_path":"/xOpDfVa83R1EdiNkWPNITfCH67e.jpg"},{"cast_id":100,"character":"Chairol Burnett (voice)","credit_id":"5d0669dc0e0a261357d0f1c9","gender":1,"id":14837,"name":"Carol Burnett","order":31,"profile_path":"/uUN23A8WtaLcItllwFADuyqF8Ci.jpg"},{"cast_id":101,"character":"Carl Reineroceros (voice)","credit_id":"5d066a180e0a260eddcd7316","gender":2,"id":1895,"name":"Carl Reiner","order":32,"profile_path":"/AtDqJQkl7dH2m19kPio3cosfbes.jpg"},{"cast_id":102,"character":"Margaret the Store Owner (voice)","credit_id":"5d066a4f0e0a266424cb6274","gender":1,"id":35515,"name":"June Squibb","order":33,"profile_path":"/2QDE0wZ9B9a4Kd2S4e9XP65agrf.jpg"},{"cast_id":104,"character":"Old Timer (voice)","credit_id":"5d066a9c0e0a26511fcb14c4","gender":2,"id":24320,"name":"Alan Oppenheimer","order":34,"profile_path":"/5UAa7wSnoh6JwiAKciCCKB9dnro.jpg"},{"cast_id":105,"character":"Duke Caboom Advert (voice)","credit_id":"5d066b9f9251413f2dbdb4d2","gender":2,"id":1237,"name":"Flea","order":35,"profile_path":"/3mvSPp8RpuuPJUuNwq9PyjFu2Ug.jpg"},{"cast_id":108,"character":"Karen Beverly (voice)","credit_id":"5d0f9cfd9251417099bdd65b","gender":0,"id":1695631,"name":"Melissa Villaseñor","order":36,"profile_path":"/144VoO4M5sU0Mn4lpTOYQFAgNVO.jpg"}],
                   "crew":[{"credit_id":"5c6ebb529251417df4101543","department":"Directing","gender":2,"id":84496,"job":"Director","name":"Josh Cooley","profile_path":"/yctxPn9Nk0ACFuf4KRUbbeYSxjT.jpg"},{"credit_id":"5c9169359251410feaa6d374","department":"Sound","gender":2,"id":7885,"job":"Original Music Composer","name":"Randy Newman","profile_path":"/w0JzfoiM25nrnxYOzosPHRq6mlE.jpg"},{"credit_id":"5c9169439251410ff6a6da2d","department":"Writing","gender":1,"id":1744241,"job":"Screenplay","name":"Stephany Folsom","profile_path":"/5OAuBOYlQOeQfexrbkT0QOWBfxi.jpg"},{"credit_id":"5c9169970e0a2612ed5bac29","department":"Writing","gender":2,"id":7,"job":"Screenplay","name":"Andrew Stanton","profile_path":"/gasNitCwepbqNcYBmDHpsCgZH0I.jpg"},{"credit_id":"5c9169ca0e0a2612fc5bb454","department":"Editing","gender":0,"id":8071,"job":"Editor","name":"Axel Geddes","profile_path":null},{"credit_id":"5c9169d90e0a2612fc5bb471","department":"Art","gender":2,"id":7889,"job":"Production Design","name":"Bob Pauley","profile_path":null},{"credit_id":"5c9169e40e0a2612f15c20be","department":"Camera","gender":0,"id":7975,"job":"Director of Photography","name":"Patrick Lin","profile_path":null},{"credit_id":"5c9169f89251410fe7a6c6fa","department":"Camera","gender":0,"id":7894,"job":"Director of Photography","name":"Jean-Claude Kalache","profile_path":null},{"credit_id":"5c916a0f0e0a2612ef5c0531","department":"Crew","gender":0,"id":2268931,"job":"Supervising Technical Director","name":"Bob Moyer","profile_path":null},{"credit_id":"5c916a200e0a2612f15c211e","department":"Production","gender":2,"id":7,"job":"Executive Producer","name":"Andrew Stanton","profile_path":"/gasNitCwepbqNcYBmDHpsCgZH0I.jpg"},{"credit_id":"5c916a380e0a2612ef5c0578","department":"Production","gender":2,"id":8,"job":"Executive Producer","name":"Lee Unkrich","profile_path":"/bdTCCXjgOV3YyaNmLGYGOxFQMOc.jpg"},{"credit_id":"5c916a410e0a266237593170","department":"Production","gender":2,"id":12890,"job":"Executive Producer","name":"Pete Docter","profile_path":"/r6ngPgnReA3RHmKjmSoVsc6Awjp.jpg"},{"credit_id":"5c916a4f9251410feda6b982","department":"Production","gender":0,"id":7940,"job":"Producer","name":"Jonas Rivera","profile_path":null},{"credit_id":"5c916a760e0a2612ef5c059e","department":"Production","gender":0,"id":8103,"job":"Producer","name":"Mark Nielsen","profile_path":null},{"credit_id":"5c916b120e0a2613045c477e","department":"Visual Effects","gender":2,"id":8011,"job":"Animation Supervisor","name":"Scott Clark","profile_path":null},{"credit_id":"5c916b1fc3a368611651f73f","department":"Visual Effects","gender":0,"id":8041,"job":"Animation Supervisor","name":"Robert H. Russ","profile_path":null},{"credit_id":"5d1139430e0a262177cb1726","department":"Writing","gender":2,"id":59891,"job":"Story","name":"Martin Hynes","profile_path":null},{"credit_id":"5d113e5b0e0a26597dcb295b","department":"Writing","gender":1,"id":80591,"job":"Story","name":"Rashida Jones","profile_path":"/jjp33eRM6oavyesW0UM6XBCxQSa.jpg"},{"credit_id":"5d1144dc9251410582b8dd70","department":"Writing","gender":1,"id":225978,"job":"Story","name":"Valerie LaPointe","profile_path":null},{"credit_id":"5d128415925141524dc16ef6","department":"Writing","gender":2,"id":7879,"job":"Story","name":"John Lasseter","profile_path":"/7EdqiNbr4FRjIhKHyPPdFfEEEFG.jpg"},{"credit_id":"5d129ccbc3a3681ab62224b0","department":"Writing","gender":2,"id":78500,"job":"Story","name":"Will McCormack","profile_path":"/6rYTgjY1pD26F8szmlPD2WDQO3e.jpg"},{"credit_id":"5d1f050794d8a8311641c195","department":"Sound","gender":0,"id":1212508,"job":"Foley Mixer","name":"Scott Curtis","profile_path":null},{"credit_id":"5d1f05320328b90010d6fe5f","department":"Sound","gender":0,"id":2030398,"job":"Foley Editor","name":"James Spencer","profile_path":null},{"credit_id":"5d1f054755b0c0638e6b3e05","department":"Sound","gender":0,"id":2017200,"job":"Sound Editor","name":"Qianbaihui Yang","profile_path":null},{"credit_id":"5d99673cb04605002a435184","department":"Sound","gender":0,"id":2426156,"job":"Sound Designer","name":"Ren Klyce","profile_path":null}]}}

en este foro para extraer genere me recomendaron el sigueinte codigo

$movie = json_decode($datos_movies, true);
$genres = array_map(function($gen) { return $gen['name']; }, $movie['genres']);
echo implode(' / ', $genres);

Tengo problemas para extraer datos de "cast" y reemplazando los valores de la siguiente forma y no me funciona

$movie = json_decode($datos_movies, true);
$cast = array_map(function($gen) { return $gen['cast']; }, $movie['credits']);
echo implode(' / ', $cast);

Deseo que me muestre "Tom Hanks / Tim Allen ....."

Saludos y muchas gracias por la ayuda es un sitio web excelente.

Ya revise la respuesta 1 en PHP 7.2 y funciona muchas gracias. Una consulta extra quiero extraer los datos de credits - crew - "Director"

Para este caso solo debería mostrar "Josh Cooley"

1 respuesta 1

1

Si he entendido bien, quieres extraer todas los valores de la propiedad names que hay en los objetos.

Puedes hacerlo de una forma mucho más simple tratando el JSON como lo que es, un objeto; accediendo al array que hay dentro de credits->cast y aplicando allí array_column para buscar la propiedad name. Luego, al array resultante la aplicas implode.

Código:

#No pasamos el parámetro true, porque no hace falta. Es más claro tratarlo como objeto
$movie = json_decode($datos_movies);
#Accedemos al array que hay en cast y buscamos el valor de name
$allNames = implode(" / ",array_column($movie->credits->cast, 'name'));
echo $allNames;

Salida:

 Tom Hanks / Tim Allen / Annie Potts / Joan Cusack / Tony Hale / Keanu
 Reeves / Keegan-Michael Key / Jordan Peele / Christina Hendricks /
 Ally Maki / Don Rickles / Blake Clark / Wallace Shawn / John
 Ratzenberger / Estelle Harris / Jeff Pidgeon / Timothy Dalton /
 Kristen Schaal / Jeff Garlin / Bonnie Hunt / Bud Luckey / Madeleine
 McGraw / Jay Hernandez / John Morris / Laurie Metcalf / Patricia
 Arquette / Lori Alan / Bill Hader / Mel Brooks / Betty White / Carl
 Weathers / Carol Burnett / Carl Reiner / June Squibb / Alan
 Oppenheimer / Flea / Melissa Villaseñor

P.D.:

Nótese que este procedimiento es válido también para los géneros:

$movie = json_decode($datos_movies);
$allGenres = implode(" / ",array_column($movie->genres, 'name'));
echo $allGenres;

Salida:

Adventure / Animation / Comedy / Family / Fantasy
11
  • Ya revise en PHP 7.2 y funciona muchas gracias. un consulta extra quiero extraer los datos de credits - crew - "Director" Para este caso solo debería mostrar "Josh Cooley" Commented el 13 oct. 2019 a las 18:58
  • @MarcosAmadorBonilla puedes obtenerlo así: $firstCrew = $movie->credits->crew[0]->name; echo $firstCrew; si es que el criterio de búsqueda es obtener el nombre del primer elemento que hay en crew.
    – A. Cedano
    Commented el 13 oct. 2019 a las 22:34
  • Si me muestra la información del primer nombre que aparece en crew. lo malo es que no siempre el primer nombre es el del director. ocupo que muestre el del director. Commented el 14 oct. 2019 a las 10:54
  • @MarcosAmadorBonilla en ese caso puedes hacer algo así: $allDirectors = array_filter( $movie->credits->crew, function ($o) { return $o->job == "Director"; } ); echo $allDirectors[0]->name; Esto te devolverá un array con todos los objetos cuyo job sea igual a Director. Si son varios puedes usar implode como en los otros casos para mostrarlos separados por /. Aquí, suponiendo que es uno solo, se usa el índice 0 en la lectura.
    – A. Cedano
    Commented el 14 oct. 2019 a las 11:57
  • Hola Muchas gracias por su tiempo y por compartir su conocimiento, revise con la solución propuesta y no me funciona me da un resultado en blanco, espero alguna otra idea de como mostrar "Director". Saludos Commented el 14 oct. 2019 a las 13:55

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.