Format for working with multiple values in an a array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
KeeganWolf
Forum Newbie
Posts: 19
Joined: Thu May 14, 2009 3:13 pm

Format for working with multiple values in an a array

Post 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?
Last edited by Benjamin on Wed May 20, 2009 11:51 am, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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?
KeeganWolf
Forum Newbie
Posts: 19
Joined: Thu May 14, 2009 3:13 pm

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

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Yes, in fact, I do.
There's an example on how to use it too.
Post Reply