Jump to content

[SOLVED] Array... again


Neopyros

Recommended Posts

I have a simple problem:

    Using fgetcsv in my file to split the "," now i have arrays inside an array.

I want to search for a word, how can i return the positions of where the words were found?

 

Heres my code so far so small =(

 

 

 

 

<?php
$handle = fopen("JobModLog.txt", "r");
$search = "Date Time";

/*------------------------------------------------------------------------------------------------------*/

for($i=0;$i<=!feof($handle);$i++){
$data[$i] = fgetcsv($handle,1000, ",");
}

fclose($handle);

/*------------------------------------------------------------------------------------------------------*/

 

Any suggestions?

Newbie question, i know ._.

 

Thks

Link to comment
https://forums.phpfreaks.com/topic/63729-solved-array-again/
Share on other sites

<?php

$handle = fopen("JobModLog.txt", "r");

while (!feof($handle)) {
    $data[] = fgetcsv($handle,1000, ",");
}

fclose($handle);


$searchWord = "test";

foreach ($data as $rowKey => $line) {

  foreach($line as $columnKey => $item {

    if (strpos ($item, $searchWord)) {

      echo "The search word exists on row $rowKey, column $columnKey <br>";

    }

  }

}


?> 

Link to comment
https://forums.phpfreaks.com/topic/63729-solved-array-again/#findComment-317567
Share on other sites

Sorry but i didn't understand....

 

When you do this:

 

foreach ($data as $rowKey => $line) {

  foreach($line as $columnKey => $item){

if(strpos($item, $searchWord)) {

	echo "The search word exists on row $rowKey, column $columnKey <br>";

    }

  }

}

 

Return an error message Warning: Invalid argument supplied for foreach()....

Link to comment
https://forums.phpfreaks.com/topic/63729-solved-array-again/#findComment-317577
Share on other sites

<?php

if ($handle = fopen("test.txt", "r")) {

  while (!feof($handle) && $xxx<20) {

    $data[] = fgetcsv($handle,1000, ",");
  }


  fclose($handle);

  $searchWord = "test";

  //Iterrate through top level of array
  foreach ($data as $rowKey => $line) {

    //Itterate through the 2nd level of the array
    foreach($line as $columnKey => $item) {

      //Check if the search string exists in the data item
      if (strpos ($item, $searchWord)!==false) {

        echo "The search word exists on row $rowKey, column $columnKey <br>";

      }

    }

  }


} else {

  echo "unable to open file";

}


?> 

Link to comment
https://forums.phpfreaks.com/topic/63729-solved-array-again/#findComment-317638
Share on other sites

Woops, I had that for testing to prevent infinite loops. That can be removed.

 

Otherwise, I tested the code and it worked perfectly. Here are the two files I used to test and the results:

 

test.txt (csv file)

apple, orange, pear
grape, apple, bannana
apricot, guava, pineapple

 

test.php (php file)

<?php

if ($handle = fopen("test.txt", "r")) {

  while (!feof($handle)) {
    $data[] = fgetcsv($handle,1000, ",");
  }
  fclose($handle);

  $searchWord = "apple";

  //Iterrate through top level of array
  foreach ($data as $rowKey => $line) {
    //Itterate through the 2nd level of the array
    foreach($line as $columnKey => $item) {
      //Check if the search string exists in the data item
      if (strpos ($item, $searchWord)!==false) {
        echo "The search word '$searchWord' exists on row $rowKey, column $columnKey: $item<br>";
      }
    }
  }
} else {
  echo "unable to open file";
}

?> 

 

Results:

The search word 'apple' exists on row 0, column 0: apple
The search word 'apple' exists on row 1, column 1: apple
The search word 'apple' exists on row 2, column 2: pineapple

Link to comment
https://forums.phpfreaks.com/topic/63729-solved-array-again/#findComment-317686
Share on other sites

Heres a part of my file search word = Date Time

 

 

***** Client Settings (15/12/2006 17:49:52) *****

Unit : mm

Coordinate Display : PanelView

Display using same origin settings for bottom and side coordinates : TRUE

Panel Origin Location : Lower Left

Panel Origin Offset X : 0.00

Panel Origin Offset Y : 0.00

Board Origin Location : Lower Left

Board Origin Offset X : 0.00

Board Origin Offset Y : 0.00

**************************************************

 

Date Time, Modified User, Command, Data Type, Previous Data, Modified Data

 

And the error keep going =P

 

Edit: Somebody here asked if the problem cause isn't cause $line isn't an array... i couldn't answer it... is it right?

Link to comment
https://forums.phpfreaks.com/topic/63729-solved-array-again/#findComment-317694
Share on other sites

If I am understanding you correctly the "code" you posted above is your "JobModLog.txt" file and your search term is "Date Time". when I added that to my data file and changed the search term I ended up with this result:

 

The search word 'Date Time' exists on row 12, column 0: Date Time

 

 

Everything seems to work fine for me.

 

However a blank line at the end of the input can cause an error, so some error handling to verify that the $line is an array is a good idea:

  foreach ($data as $rowKey => $line) {
    if (is_array($line)) {
      //Itterate through the 2nd level of the array
      foreach($line as $columnKey => $item) {
        //Check if the search string exists in the data item
        if (strpos ($item, $searchWord)!==false) {
          echo "The search word '$searchWord' exists on row $rowKey, column $columnKey: $item<br>";
        }
      }
    }
  }

Link to comment
https://forums.phpfreaks.com/topic/63729-solved-array-again/#findComment-317714
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.