I am an absolute beginner with PHP. My problem is twofold, first I have a list of ancestor names and each name has 13 key => value pairs. I need my output to have a single row of headers ($keys) and mutiple rows of matching $values. My code at present repeats the header keys with each name and all the output is in one column. I think I need to exclude the tag for the headers and place them separately in the CSV.`
I have looked at similar problems in Stackoverflow and tried to adopt their resolutions without success.
include_once('_inc/simple_html_dom.php');
//create an array of all URLs we need to target
$pages = array(
"http://fammilytree.loc/surnames/A%20Surnames/Adams/James%20Adams%201864.php",
"http://familytree.loc/surnames/A%20Surnames/Adams/John%20Adams%201901.php",
"http://familytree.loc/surnames/A%20Surnames/Anderson/Jeals%20Anderson%201718.php",
"http://familytree.loc/surnames/A%20Surnames/Arnett/Ann%20Arnett%201781.php");
//We are initializing an array to hold the values of each <TD> from an ancestor's page
$ancestor = array();
?>
<html>
<body>
<?php
//this loop goes through our "pages" array and then for each page in the array...
foreach($pages as $page){
//...calls the function which pulll all of the HTML from the page into an array
$html = file_get_html($page);
//I think we need a multi-dimensional array, using name of ancestor as index for next array
// $ancestor = array()
// then FOR EACH element in the array of HTML, find the TD value
foreach($html->find('td') as $element){
//and put the value in our Ancestor array
$ancestor[] = $element->innertext.",";
}
}
//display the full Ancestor array to the page
print_r($ancestor);
//OPen the file called FILE.csv with 'w'=write permissions
$handle = fopen('export.csv', 'w');
foreach ($ancestor as $fields) {
fputcsv($handle, (array) $fields);
}
fclose($handle);
?>
</body>
</html>```
question from:
https://stackoverflow.com/questions/66068651/why-is-fputcsv-output-a-single-column-instead-of-multiple-columns-and-rows 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…