Page 1 of 1

Format for working with multiple values in an a array

Posted: Wed May 20, 2009 11:43 am
by KeeganWolf

Code: Select all

<?php
$fileContent = file_get_contents("Product/tblPmixDetail");
 
//Convert Windows CR to Linux
$fileContent = str_replace("\r\n","\n",$fileContent);
 
//Split file by finding right CR.
$expr="/\n(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
$rows=preg_split($expr,trim($fileContent));
unset($fileContent); //Free up some memory
$data = array();
foreach($rows as $row){
  //Find right coma
  $expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
  $results = preg_split($expr,trim($row));
  //Remove quote
  $data[] = preg_replace("/\"(.*)\"/s","$1",$results);
}
//Free up some memory
unset($rows); 
$count = count($data);
for ($i = 0; $i < $count; $i++) {
    unset ($data[$i][0,1,3,4,5,6,7,8,9,10,12,13]);  <------Is this right?
}
//Show the result
echo "<pre>";
print_r($data);
echo "</pre>";
 
 
?>
 
On line 23, I have several reasons I'm going to need to work with multiple values in a multidimensional array at the same time, but I don't know the format to use
Hence [0,1,2,3,4.......] The numbers are names of the other arrays
Is that not correct?

Re: Format for working with multiple values in an a array

Posted: Wed May 20, 2009 12:12 pm
by requinix
No, it's not. That syntax isn't valid anywhere in PHP.

If you don't want those, why not just ignore them?

And by the way, it looks like you're dealing with a CSV file. You know there's a function specifically designed to read in CSV data from a file?

Re: Format for working with multiple values in an a array

Posted: Wed May 20, 2009 1:42 pm
by KeeganWolf
I wasn't sure, kinda new to php. I am dealing with a csv file, I just found this quick snippet will take the values and arrange them into an array like I need. Did you have a better suggestion?
Thanks

Re: Format for working with multiple values in an a array

Posted: Wed May 20, 2009 2:10 pm
by requinix
Yes, in fact, I do.
There's an example on how to use it too.