[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Create Unique Array Based on Array Keys in File

I have data in a file that I use 'explode' get the keys/values.

1=apple,2=lettuce
3=rice,4=juice
1=orange,5=yogurt

I will need to loop through each line to get a unique list of the array keys.

how do I write the foreach rule in order to put the unique list of keys in one array?

thanks in advance.
0
deharvy
Asked:
deharvy
1 Solution
 
Pratima PharandeCommented:
0
 
yadayaCommented:
<?php
$file = fopen("keys.txt", "r") or exit("Unable to open file!");
$keys = array();
while(!feof($file))
  {
      $line = fgets($file);
      $line = explode(",", $line);
      foreach ($line as $item)
      {
            $item = explode("=", $item)
            $keys[$item[1]] = $item[0];            
      }
  }
print_r($keys); //TESTING
fclose($file);
?>
0
 
deharvyAuthor Commented:
Thank you.
0
 
Ray PaseurCommented:
Let's try to be clear on the terminology.  Are the numbers the "keys" here?  So that if you got unique keys you would have 1,2,3,4,5?  Would you care about apple vs orange (they both have the same key)?  Give us a little more to go on here, because the question looks very theoretical (even academic) and I have a feeling that there are practical matters that might influence the data structure.  What I am looking at here looks to me like it would be better suited to be XML, instead of a string of number=fruit pairs.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now