entre Desarrolladores

Recibe ayuda de expertos

Registrate y pregunta

Es gratis y fácil

Recibe respuestas

Respuestas, votos y comentarios

Vota y selecciona respuestas

Recibe puntos, vota y da la solución

Pregunta

1voto

Pasar json a csv usando php

Hoy me encontré con una duda realizó una llamada a una página y me regresa un .json que contiene lo siguiente :

{"response":{"count :378,"items":[{"id":217775120,"first_name":"Hernán","last_name":"Báez","online":0},{"id":300583272,"first_name":"Emiliano","last_name":"Grecco","online":0},{"id":195876267,"first_name":"Padisc","last_name":"Vintage","online":0},{"id":188512977,"first_name":"Espanol","last_name":"Network","online":0},{"id":286516269,"first_name":"Dani","last_name":"Dan","online":0},{"id":526309153,"first_name":"Leonardo","last_name":"Sánchez","online":0},{"id":318847339,"first_name":"Canalpelis","last_name":"Online","online":0},{"id":282800022,"first_name":"Josemaria","last_name":"Nieto Tierra","online":0},{"id":264060759,"first_name":"Miguel","last_name":"Zavarof","online":0},{"id":173457305,"first_name":"Teodoro","last_name":"Lopez","online":0},{"id":520902659,"first_name":"Blue","last_name":"Love","online":0},{"id":162523459,"first_name":"Marcos","last_name":"Férnandez","online":0},{"id":502843767,"first_name":"Guillermo","last_name":"Calderon","online":0},{"id":494597552,"first_name":"Carina-Eugenia","last_name":"Calonga","online":0},{"id":336520601,"first_name":"Román","last_name":"Ruiz","online":1},{"id":357708754,"first_name":"Ciudad","last_name":"Cultural","online":0},{"id":421137904,"first_name":"Rafa","last_name":"Castro","online":0},{"id":353603786,"first_name":"Ed","last_name":"Vallejo","online":0},{"id":262003746,"first_name":"Alejandro","last_name":"Frutos-Comparetto","online":0},{"id":538493295,"first_name":"Raúl","last_name":"Caraveo Toledo","online":0},{"id":400782191,"first_name":"Xwall","last_name":"Ricardi","online":0},{"id":325434135,"first_name":"Iván","last_name":"Villar","online":0},{"id":311177154,"first_name":"Francisco-Javier","last_name":"Calzada-Lopez","online":0},{"id":210222972,"first_name":"Gen","last_name":"Iero","online":0},{"id":169051516,"first_name":"Pepe","last_name":"Tansolo","online":0},{"id":331548225,"first_name":"Jose","last_name":"Gomez","online":0},{"id":312662742,"first_name":"Anabel","last_name":"Rodriguez","online":0},{"id":288056567,"first_name":"Franco","last_name":"Liotta","online":0},{"id":420661927,"first_name":"William","last_name":"Clinton","online":0},{"id":349933730,"first_name":"Viktor","last_name":"Grigorievich","online":0},{"id":164133743,"first_name":"Peliculaskid","last_name":"Com","online":0},{"id":335518207,"first_name":"César","last_name":"Eduardo","online":0},{"id":205049120,"first_name":"Carlos","last_name":"Salgado","online":0},{"id":432792923,"first_name":"Emilio","last_name":"Gomez-Vera","online":0},{"id":199299884,"first_name":"Aleister","last_name":"Crowley","online":0},{"id":143625934,"first_name":"Enrique","last_name":"Amaya","online":0},{"id":365862403,"first_name":"Nestor","last_name":"Castro","online":0},{"id":220163416,"first_name":"Mia","last_name":"Solorzano","online":0},{"id":366348914,"first_name":"Agenda","last_name":"Sociocultural","online":0},{"id":336166138,"first_name":"Ana","last_name":"Rang","online":0},{"id":285677321,"first_name":"Luna","last_name":"Gelida","online":0},{"id":144629714,"first_name":"Tonalli","last_name":"Arteaga","online":0},{"id":526854716,"first_name":"Miguel","last_name":"Martínez","online":0},{"id":274247897,"first_name":"Jorge","last_name":"Carmona","online":0},{"id":494721586,"first_name":"Johnbritto","last_name":"Britto","online":0},{"id":524461342,"first_name":"Richard","last_name":"Mazzoccone","online":0},{"id":465222660,"first_name":"Mauro","last_name":"Buitrago","online":0},{"id":231767284,"first_name":"Silvia","last_name":"Cañadas","online":0},{"id":215306065,"first_name":"Darío","last_name":"Billani","online":1},{"id":172382488,"first_name":"Chimo","last_name":"Dimension","online":0},{"id":253572519,"first_name":"Carmen","last_name":"Oliveros","online":0},{"id":57067305,"first_name":"David","last_name":"Bofill","online":0},{"id":529416825,"first_name":"Sibila","last_name":"Crombas","online":0},{"id":273480697,"first_name":"Sandra","last_name":"Martinez Mancebo","online":0},{"id":184056171,"first_name":"Estefania","last_name":"Farias","online":0}]}}

E intentado recoger los datos de esta manera :

$A = json_decode($respon,true);

$contenido="";
foreach($A["items"] as $row){ 
    $contenido .= $row["id"].PHP_EOL; 
}

$A contiene el array esa prueba es solo para los id .igual me gustaría guardar los datos de esta manera .

$datos .= 'Id, 
first_name, last_name';

Luego guardar la variable $Datos en usuarios.Csv.

Espero avería sido claro en lo que busco . y gracias por adelantado

1 Respuesta

1voto

Botxtrem-Solutions Puntos1990

Aquí les dejo mi propia respuesta simplificado las otras respuestas .


$posts = $array['response']['items'];

foreach($posts as $P){

$RN = $P['id'];
echo $RN. '<br>';
}

Usando array_column() el ciclo es mas rápido que foreach() ejemplo


$id = array_column( $array['response']['items'], 'id');
echo  $id;

Otra forma es accediendo al id directamente ejemplo:

echo $array['response']['items']['0']['id'];

Igual es justo recordar otra manera fácil de lograrlo usando implode();


echo implode(" ",$array['response']['items']['0']['id']);

Saludes y gracias totales

0voto

Peter comentado

Gracias por compartir la solución.

Por favor, accede o regístrate para responder a esta pregunta.

Otras Preguntas y Respuestas


...

Bienvenido a entre Desarrolladores, donde puedes realizar preguntas y recibir respuestas de otros miembros de la comunidad.

Conecta