I have some code that reads in data from a flat file, and explodes it into a 2-dimensional array.
I consider a row to be invalid if it does not include a full set of data (eg. no cost). But I'm not sure how, or whether it's possible to use array_filter to remove invalid rows.
//Contents of file: data.txt
//Data File (Invalid data line)
// (Invalid, no data)
//Stock No|Description|Image|Cost
//BK-101|Big Book of Stories|bk-101.jpg|10.00
//BK-327|How to Read Books|bk-327.jpg|9.50
//BK-512|The World's Best Books|bk-512.jpg|19.50
// (Invalid, no data)
<?php
$delim_field="|";
$filename = 'data.txt';
$data_rows = file($filename); //Read file into array
Echo "Data_row file contents:<br><pre>";
print_r($data_rows);
foreach ($data_rows as $row) { //Split row data into columns
$array[] = explode($delim_field,$row);
}
Echo "<p>Exploded array:<br>";
print_r($array);
?>
Can I now filter out $array rows [0] [1] and [6] since $array[?][3] are null, using array_filter, or must I loop through and test each row?
Regards,
Ian Tresman