Working with data - newbie array advice

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
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Working with data - newbie array advice

Post by is_blank »

Say I have a text file in the format
jul 4
Picasso
Three Musicians
---
jul 5
Vincent Van Gogh
Bedroom at Arles
---
jul 6
Rembrandt
The Return of the Prodigal Son
...
etc.
The subject matter here is kind of arbitrary, but suppose I'd like to be able to run through such a file, find today's date, and drop the data afterward into variables ($artist, $painting) so I could continue to work with them -- what would be the best way to accomplish that?

I thought maybe some kind of array like:

Code: Select all

$daily_art = array(
     "jul 4" => array("artist" => "Picasso", "painting" => "Three Musicians"),
     "jul 5" => array("artist" => "Van Gogh", "painting" => "Bedroom at Arles"),
     "jul 6" => array("artist" => "Rembrandt", "painting" => "The Return of the Prodigal Son"),
     ...)
And then using array_search to find date('M j') in there, or something like that. The problem is, I'm just finding my way around arrays, let alone two-tiered associative arrays like that! I've been fumbling through php.net for a while, and I'm not really sure how to get started here.

I'm wondering (1) is this the best way to go about this? (2) If so, how in the world can I generate that kind of array from my text file? and (3) If I can use array_search to find the date key (which doesn't really seem to be working for me right now...), is it pretty easy to extract 'artist' and 'painting' from there?

Thanks!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

$start_str = date('M j');
$start_len = strlen($start_str);

$fp = fopen('file', 'r');
while (!feof($fp))
{
  $line = fgets($fp);
  if (!$line)
  {
    break;
  }

  // we found the line with the current date
  if (substr($line, 0, $start_len) == $start_str)
  {
    break;
  }
}


$arts = array();

while (!feof($fp) && $line)
{
  $art = array();
  list($month, $day) = explode(' ', $line);
  // do something to convert 'jul' to 7...
  $artist = fgets($fp);
  $painting = fgets($fp);
  $skip = fgets($fp);
  $line = fgets($fp);
  $arts[$month][$day] = array('artist' => $artist, 'painting' => $painting);
}

fclose($fp);
Now you have:
$arts[7][4] => array('artist' => 'Picasso', 'painting' => 'Three');
...
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Post by is_blank »

Thanks! That gets me on the right track, I think. I'm still not really understanding reading from files, though... If someone has time, could you explain to me why taking this:

Code: Select all

while (!feof($fp) && $line)
{
  $art = array();
  list($month, $day) = explode(' ', $line);
  // do something to convert 'jul' to 7...
  $artist = fgets($fp);
  $painting = fgets($fp);
  $skip = fgets($fp);
  $line = fgets($fp);

  echo $artist;
  echo $painting;
  echo $skip;
}
gives me this -->
Picasso
Three Musicians
---
Van Gogh
Bedroom at Arles
---
Rembrandt
The Prodigal Son's Return
instead of this -->
Picasso
Van Gogh
Rembrandt
Three Musicians
Bedroom at Arles
The Prodigal Son's Return
---
---
---
Also, how can I get at those elements once they're stored? I can see everything in there if I do a print_r($arts);, but can I do something like print $arts[Jul][4]? I guess what I'm asking is: how can I dig into multi-dimensional arrays like this to pull out specific values?

Thanks for all the help! Sorry if I'm a little slow... :oops:
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Post by is_blank »

Also, how can I get at those elements once they're stored? I can see everything in there if I do a print_r($arts);, but can I do something like print $arts[Jul][4]? I guess what I'm asking is: how can I dig into multi-dimensional arrays like this to pull out specific values?
Sorry! Got this part...the code was leaving some blank space after $day in this line

Code: Select all

list($month, $day) = explode(' ', $line);
So it was messing up the creation of the final array. For lack of anything better, I just threw in a trim():

Code: Select all

$arts[$month][trim($day)] = array('artist' =>$artist, 'painting' => $painting);
So digging out the data works as I had expected.

Answers to my previous question still gratefully accepted, though! :wink:
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

is_blank wrote:

Code: Select all

while (!feof($fp) && $line)
{
  $art = array();
  list($month, $day) = explode(' ', $line);
  // do something to convert 'jul' to 7...
  $artist = fgets($fp);
  $painting = fgets($fp);
  $skip = fgets($fp);
  $line = fgets($fp);

  echo $artist;
  echo $painting;
  echo $skip;
}
gives me this -->
Picasso
Three Musicians
---
Van Gogh
Bedroom at Arles
---
Rembrandt
The Prodigal Son's Return
Well, you read the file line by line... each fgets will read in a line. After the first loop you have already read "the date" - line.

So all you need now is "artist", "painting", "----" lines.

And now you prepare the next record (5lines), thus you need to make sure the "date line" is already is $line...

Now you are ready to move back to the beginning of the loop ;)
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Post by is_blank »

Ok. I get it. So every time fgets() is called, it jumps the pointer in the file up one line, right? Sorry--that's so simple; It just never occurred to me that it would keep track of where it was in the source file like that. (Can you tell I haven't done this much? :wink:)

Thanks again!
Post Reply