Get the contents of an array or PHP object

1

I improvised a code that reads the XML files of a folder, one after another, and retrieves the information of some specific fields (title and summary), and I want to pass this information to a file CSV to one row per file (one column with titles and another with summaries).

Here it is complete:

<?php
$fp = fopen("C:/*****/PHP/METADAT.csv","a"); 

$directorio = ('C:/Users/***/xml');

$ficheros1 = scandir($directorio);
$ficheros1 = array_diff($ficheros1,array('.','..')); 

$cantidad = count($ficheros1);

$i = 2;
while ($i <= $cantidad):

  $carga = simplexml_load_file('C:/Users/***/xml');
  /*acceder a las rutas XML que nos interesan (XPATH)*/
  $titulo = $carga->xpath(
    '/MD_Metadata/identificationInfo/MD_DataIdentification/citation/CI_Citation/title'
  );
  $resumen = $carga->xpath('/MD_Metadata/identificationInfo/MD_DataIdentification/abstract');

  /*volcar el contenido en CSV*/

  $lista[$i] = array($titulo,$resumen);
  $i++;
endwhile;        //fin del bucle while

foreach ($lista as &$campos) {
  fputcsv($fp, $campos);
}
fclose($fp);
?>

The problem is that the fputcsv($fp,$campos) creates a CSV that contains the word Array many times, because when doing data loading in $titulo and% % co considers them objects $resumen or something like that inside they have the desired information, when I really only want the textual value contained in 'title' and 'abstract' from within my XML .

Can I recover that value in some simple way?

    
asked by Antonio999992 27.01.2017 в 15:04
source

1 answer

0

first, xpath returns an array , so you should take one of those elements. If you have, exactly 1, just read the position [0] . Then, you must cast a SimpleXMLObject to string . Something like:

$titulo = (string)$carga->xpath('/MD_Metadata/identificationInfo/MD_DataIdentification/citation/CI_Citation/title')[0];
$resumen = (string)$carga->xpath('/MD_Metadata/identificationInfo/MD_DataIdentification/abstract')[0];

I hope to help you! greetings

    
answered by 27.01.2017 / 15:12
source