1

I have a flat file called data.txt. Each line contains four entries.

data.txt

blue||green||purple||primary
green||yellow||blue||secondary
orange||red||yellow||secondary
purple||blue||red||secondary
yellow||red||blue||primary
red||orange||purple||primary

And I tried this to find out if the variable "yellow" exists as the FIRST entry on any of the lines:

$color = 'yellow';

$a = array('data.txt');

 if (array_key_exists($color,$a)){
 // If true
   echo "$color Key exists!";
   } else {
 // If NOT true
   echo "$color Key does not exist!";
   }

but it is not working as expected. What can I change to get this going? Thanks....

5 Answers 5

2

The following utilizes preg_grep, which performs a regular expression search on each element of an array (in this case, the lines of the file):

$search = 'yellow';
$file = file('file.txt');

$items = preg_grep('/^' . preg_quote($search, '/') . '\|\|/', $file);

if(count($items) > 0)
{
   // found
}
Sign up to request clarification or add additional context in comments.

2 Comments

Though that would false positive match any prefix of other words. If the example is just contrived. ie. $search = 'yell' would match yellow as the first word, a change to the regex would be required to ensure a separator followed or a line terminal.
@Orbling: Thanks for that overlook. Updated.
0
$fh = fopen("data.txt","r");
$color = 'yellow';
$test = false;

while( ($line = fgets($fh)) !== false){
    if(strpos($line,$color) === 0){
        $test = true;
        break;
    }
}

fclose($fh);
// Check $test to see if there is a line beginning with yellow

Comments

0

The line:

$a = array('data.txt');

Only creates an array with one value in it: 'data.txt'. You need to read and parse the file first before checking for the values.

Comments

0

Well, that's not how you load a separated list of data from a text file in to an array.

Also array_key_exists() checks keys only, not the values of an array.

Try:

$lines = file('data.txt', FILE_IGNORE_NEW_LINES);

$firstEntries = array();

foreach ($lines as $cLine) {
   $firstEntries[] = array_shift(explode('||', $cLine));
}

$colour = 'yellow';

if (in_array($colour, $firstEntries)) {
   echo $colour . " exists!";
} else {
   echo $colour . " does not exist!";
}

2 Comments

This code will return "exists' even if yellow is NOT the first entry on the line.
@mobilestimulus: Didn't notice the "FIRST" constraint, I'll update it.
0

The data in your file is not loaded into $a. Try

$a = explode("\n", file_get_contents('data.txt'));

to load it and then check each line with:

$line_num = 1;
foreach ($a as $line) {
   $entries = explode("||", $line);
   if (array_key_exists($color, $entries)) {
      echo "$color Key exists! in line $line_num";
   } else {
      echo "$color Key does not exist!";
   }
   ++$line_num;
}

2 Comments

This code displays: Warning: explode() expects at least 2 parameters, 1 given in /www/tests/mobilestimulus/array_tests/index1.php on line 10 Warning: Invalid argument supplied for foreach() in /www/tests/mobilestimulus/array_tests/index1.php on line 13
@mobilestimulus I wrote this code without testing it. I missed the first argument delimiter ("\n" - the new line character) of the first explode. It should work properly now.

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.