Jump to content

Manipulating an array


jcbarr

Recommended Posts

Okay, this question may be a lot to ask and I thank anyone who tries. Let me start by saying I don't know if I am even going about this the right way, so if there is an easier way to do this then I am up for it!

I want to pull data from an already existing HTML page.
http://csbl.net/league_html/leaders.htm

I have done this and put it in to an array, thinking that was the best way to start with this. Here is the page that displays the array, and below it the code that I used to put it into the array
http://cbl-baseball.com/grab.php

[code]<?php
$filename = "http://csbl.net/league_html/leaders.htm";
$file = file($filename);
$data = array();

foreach ($file as $line) {
  $data[] = strip_tags($line);
}

echo '<pre>';
print_r($data);
echo '</pre>';
?>[/code]

Of course I know that all that does is print the array, but at least I know that it is in there. What I want to do is then display only certain portions of the data.

The output that I want is only the leader in certain categories. An example of how I want the output to look is like this;

[code]Regional Governors League

Batting Average
B. Siebert (MAR)            .365

Home Runs
S. Hodges (CMP)              29

Runs Batted In
S. Hodges (CMP)              76

Stolen Bases
B. Lee (SPR)                  45

Wins
F. Callahan (QPJ)            10

ERA
A. Baker (ORL)              .64

Srike Outs
M. Harvel (SPR)              68

Saves
B. Butcher (MAR)              14
[/code]

I would want to print that for both leagues listed in the array, but I don't want any other information printed.

Like I said, I don't know if I am even heading down the right road with this. Any help or guidance would be awesome. Thanks again to anyone willing to help me with this.
Link to comment
https://forums.phpfreaks.com/topic/33511-manipulating-an-array/
Share on other sites

Oh!
I see someone else has also taken up the quest (I am sorry).
Well, I was bored and it looked like a fun question so I made a suggestion on how to parse the text.
I mainly used the fact that it was split up with multiple spaces.

So, it creates a multidimensional array
$parsed_league
You can use print_r($parsed_league); if you want to see how it is structured.
I also made an example of how to output it with spaces to separate names with their "values".

Maybe it can be done more easilly, but I am pretty new to php :P

Good luck!

(Oh bummer! I just noticed I totally forgot to output what category they were the leader of. I edited it to do it now)


[code]<?php
$filename = "http://csbl.net/league_html/leaders.htm";
$file = file($filename);
$data = array();

foreach ($file as $line) {
  $data[] = strip_tags($line);
}


$groups = array('Season Batting Leaders', 'Season Pitching Leaders');

$league = '';
$types = array();
$parsed_list = array();
$wait_for_empty = true;

foreach($data as $row)
{
$row = trim($row);
if( empty($row) )
{
$wait_for_empty = false;
}
else
{
foreach($groups as $testgroup)
{
if( strpos($row, $testgroup) !== false )
{
$league = trim(str_replace($testgroup, '', $row)); // Fetches which league
$types = array(); // Should not be needed. Just precaution
$wait_for_empty = true;
}
}
if( !$wait_for_empty && !empty($league) )
{
if( empty($types) ) //
{
// trim() removes the spaces at the start and end of the row.
// The preg_replace turns any multiple of spaces into double space, which is the separator in the explode
$types = explode( '  ', preg_replace('/\s{3,}/', '  ', trim($row)));
}
else
{
foreach($types as $type)
{
$row = preg_replace('/^\s*1?\s+/', '', $row);  // Removes the spaces and number at start.
$length = strpos($row, '  ');
$parsed_list[$league][$type]['name'] = substr($row, 0, $length);
$row = substr($row, $length); // Cut away the name from the string

$row = ltrim($row);
$length = strpos($row, '  ');
$length = empty($length) ? strlen($row) : $length; // To also grab the last value if spaces are missing

$parsed_list[$league][$type]['value'] = substr($row, 0, $length);
$row = substr($row, $length);
}

$types = array(); // Clear the array as we've collected the desired information
$wait_for_empty = true;  // Don't parse any row until we hit an empty line
}
}
}
}

echo '<pre>';
foreach($parsed_list as $name=>$league)
{
echo "\n".$name."\n";
foreach($league as $type_name=>$type)
{
echo "\n".$type_name."\n";
echo $type['name'].str_repeat(' ', 40 - strlen($type['name'].$type['value'])).$type['value']."\n";
}
echo "\n";
}
echo '</pre>';
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/33511-manipulating-an-array/#findComment-156920
Share on other sites

Ah, well, nevermind I guess. Yours seems to do exactly what the OP asked for. I was taking a more proactive approach to put ALL the data into a processed array. Because, down the road you will want to do something with that data as well. And, it would be very simple to get just the first person from each stat in this manner.

Anyway, here is what I got so far (just add this to your page and change the name of $dataArray in the first foreach with the name of your array):
[code]<?php

$statList = array ();
$statList["Batting"] = array(
    "Batting Average (Avg)","Slugging percentage (SLG)","Hits (H)",
    "Doubles (2B)", "Triples (3B)", "Home runs (HR)",
    "Runs (R)", "Runs batted in (RBI)", "Runs created (RC)",
    "Bases on balls (BB)", "Strikeouts (SO)", "Stolen bases (SB)");
$statList["Pitching"] = array(
    "Wins (W)", "Losses (L)", "Saves (Sv)",
    "Earned run average (ERA)", "Batting average (Avg)", "Slugging percentage (SLG)",
    "Innings pitched (IP)", "Strikeouts (SO)", "Bases on balls (BB)",
    "Hits per 9 IP (H/9)", "Strikouts per 9 IP (SO/9)", "Walks per 9 IP (BB/9)");


$newArray = array();
$league = "";
$stats = "";

foreach ($dataArray as $line) { // <== CHANGE THE ARRAY NAME

  if(trim($line)) {

    //Set the league value if line = "Regional Governors League"
    if (strpos($line, "Regional Governors League") !== false) {
      $league = "Regional Governors League";

      //Determine the section for the stats
      if (strpos($line, "Season Pitching Leaders") !== false) { $section = "Pitching"; }
      elseif (strpos($line, "Season Batting Leaders") !== false) { $section = "Batting"; }

    //Set the league value if line = "Sith League"
    } elseif (strpos($line, "Sith League") !== false) {
      $league = "Sith League";

      //Determine the section for the stats
      if (strpos($line, "Season Pitching Leaders") !== false) { $section = "Pitching"; }
      elseif (strpos($line, "Season Batting Leaders") !== false) { $section = "Batting"; }

    //Process the line
    } else {

      //Don't process if no league or section set
      if ($league && $section) {

        //Check for new stats section
        foreach ($statList[$section] as $statName) {
          $found = strpos($line, $statName);
          if ($found !== false) {

            //Put current processing stats into array
            if ( $found <= 38 ) { $stats[0] = $statName; }
            if ( $found > 38 && $found < 76 ) { $stats[1] = $statName; }
            if ( $found >= 76 ) { $stats[2] = $statName; }
            $newStats = true;

          }
        }

        //Check for stat data (if not new stats line)
        if (!$newStats) {

          //Put the stat data into new array
          $newArray[$league][$section][$stats[0]][] = trim(substr($line, 3,  34));
          $newArray[$league][$section][$stats[1]][] = trim(substr($line, 41, 34));
          $newArray[$league][$section][$stats[2]][] = trim(substr($line, 79, 34));

        } else {
          $newStats = false;

        }
      }
    }
  }
}

echo "<pre>";
print_r($newArray);
echo "</pre>";

?>[/code]
The new array looks like this:
[code]Array
(
    [Regional Governors League] => Array
        (
            [Batting] => Array
                (
                    [Batting Average (Avg)] => Array
                        (
                            [0] => B. Siebert (MAR)            .365
                            [1] => S. Hodges (CMP)            .353
                            [2] => L. Leonardson (SPR)        .351
                            [3] => J. Strummer (VAL)          .350
                            [4] => B. Sommers (KMZ)            .346
                            [5] => R. Piper (STM)              .340
                            [6] => A. Corriden (SPR)          .338
                            [7] => A. Moeller (KMZ)            .336
                            [8] => S. Mannara (CMP)            .322
                            [9] => C. Layte (KMZ)              .315
                        )

                    [Slugging percentage (SLG)] => Array
                        (
                            [0] => S. Hodges (CMP)            .700
                            [1] => R. Piper (STM)              .648
                            [2] => A. Moeller (KMZ)            .633
                            [3] => L. Leonardson (SPR)        .622
                            [4] => B. Siebert (MAR)            .620
                            [5] => S. Kuwabatake (MAR)        .600
                            [6] => J. Strummer (VAL)          .556
                            [7] => B. Sommers (KMZ)            .552
                            [8] => S. Mannara (CMP)            .547
                            [9] => A. Corriden (SPR)          .535
                        )
                    etc.
                    etc.
                    etc.[/code]
The only things left would be to actually parse the data such that:
1. The name is separate from the statistic
2. To add the rankings such that multiple players are "grouped" together when they have the same ranking.
Link to comment
https://forums.phpfreaks.com/topic/33511-manipulating-an-array/#findComment-156943
Share on other sites

Sorry for "busting in". But yes, I was also thinking about parsing it all, but I realized it would grow more complicated unless one would rely on a constant number of characters per person and value (which I wasn't sure would be reliable).

Fun though to see how another person would face the same problem :)
Link to comment
https://forums.phpfreaks.com/topic/33511-manipulating-an-array/#findComment-156956
Share on other sites

Just a few questions and I promise that I might leave you alone...lol

In this secion;

[code]
//Put current processing stats into array
if ( $found <= 38 ) { $stats[0] = $statName; }
if ( $found > 38 && $found < 76 ) { $stats[1] = $statName; }
if ( $found >= 76 ) { $stats[2] = $statName; }
$newStats = true;[/code]

What is the significance of the 38 and the 76?

And in this part;

[code]//Put the stat data into new array
$newArray[$league][$section][$stats[0]][] = trim(substr($line, 3, 34));
$newArray[$league][$section][$stats[1]][] = trim(substr($line, 41, 34));
$newArray[$league][$section][$stats[2]][] = trim(substr($line, 79, 34));
[/code]

What are the 3, 34
41, 34
and 79,34?

I'm assuming these are character positions in the line? Or am I totally wrong here?
Link to comment
https://forums.phpfreaks.com/topic/33511-manipulating-an-array/#findComment-156995
Share on other sites

Damnit! I just spent 15 minutes typing in a long response to accidentally hit the wrong key and lose everything. Oh well, to paraphrase what I had before...

Yes, those numbers are for the character positions. The "data" is in columns of 38 characters. So, in the first section when I found a "section header" in one of the lines I wanted to determine which column it was in. So, if it existed in the first 38 characters then I put it in the first position in the $stats array (actually I should have used 37, but there is plenty of space between the data columns).

In the next section of code you posted I was doing something similar. However, here I was stripping out the "rank" data for each entry which existed in the first 3 characters. So, when using the substr() function I started at the 3rd character for each column and took the first 34 characters.

The problem with all this is that we do not have any knowledge of how the page with the data is created. It could be that some of the assumptions made will not always be true and the functions will break. In hindsight I would probably include the rank data and then parse that and the name and statistic into separate data elements. Then, if needed, you could easily import into a database.

Anyway, to display the data in the manner you asked for originally you could do this:

[code]<?php
foreach ($newArray as $leagueName => $leagueStats) {
    echo "<h2>" . $leagueName . "</h2>";
    foreach ($leagueStats as $sectionName => $sectionStats) {
        echo "<h4>" . $sectionName . "</h4>";
        foreach ($sectionStats as $statName => $stats) {
            echo $statName . "<br>";
            echo $stats[0];
        }
    }
}
?>[/code]

Of course, one problem is that if two, or more,  players were tied for 1st, only the first player would be displayed. As I stated before, I would probably group players that were tied so that I could show all the #1 players for a particular stat if there were multiples.
Link to comment
https://forums.phpfreaks.com/topic/33511-manipulating-an-array/#findComment-157042
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.