0
ha15rs,250,home2.gif,2
ha36gs,150,home3.gif,1
ha27se,300,home4.gif,4
ha4678,200,home5.gif,5

when i turn this text file into array $handle, how can i test the second field (250,150,300,200)

i want to check if its below 100, if it is then display the row!!

if $secondfield < 100

then echo 

how could i manipulate the array to do this in php thanks ?

4 Answers 4

1

Simple..

$h = file("file_name");

for ($x = 0; $x < count($h); $x++)
{
   $a = explode(",", $h[$x]);
   if ($a[1] < 100)
        echo $h[$x];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I don't understand what you mean
1

I presume you have each line in the file as an element in your array. In that case:

$data = array_map(function($e){ return explode(',', $e); }, $array);
$desiredData = array_filter(function($e){ return $e[1] < 100; }, $data);

Comments

0

Explode every string, so you have four arrays:

foreach($strings as $string){
  $arr = expode(',', $string);

  if( intval($arr[1]) < 100 ){
    echo $string;
  }
}

Comments

0

Assuming you have a csv (I copied and modified some code from the php manual):

if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($line = fgets($handle)) !== FALSE) {
        $data = str_getcsv($line);
        if ($data[1] < 100)
            echo $line;
    }
    fclose($handle);
}

1 Comment

but you can't get the whole line to echo later (you could build it again but it's faster to have it ready)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.