Jump to content

Array manipulation, removing and trimming


jcbarr

Recommended Posts

Okay I have an array that looks like this;

[code]Array
(
    [0] =>
    [1] => CFL, 2007 season, Week 17
    [2] => CFL
    [3] => Retirements
    [4] => Arizona Wranglers
    [5] => Retired:
    [6] => Gary Irvin (P), 12 seasons
    [7] => Reuben Railey (CB), 11 seasons
    [8] => Jay Butcher (TE), 11 seasons
    [9] => Steve Newton (HB), 11 seasons
    [10] => Eric Moore (DE), 10 seasons
    [11] => Denver Gold
    [12] => Rich Masters (WR), 13 seasons
    [13] => Redford Wallach (P), 12 seasons
    [14] => David Atkins (QB), 1 season
    [15] => Junior Wolford (DE), 11 seasons
    [16] => Los Angeles Express
    [17] => Robert Goff (CB), 10 seasons
    [18] => Leonard Clifton (CB), 11 seasons
    [19] => Rob Fullingto (CB), 12 seasons
    [20] => Roy Brown (WR), 9 seasons[/code]

There is a lot more to it but that should give you all the info you need. I'm creating the array with the following php code;

[code=php:0]<?php
$filename = "http://cfl.cbl-baseball.com/stats/2007/retire.htm";
$file = file($filename);
$data = array();

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

$result = array_unique($data);

foreach ($result as $line) {
  $result1[] = trim($line);
}

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

So here is what I want to do. I want remove every entry in the array that is not a player. So if the value contains the word seasons then I want to keep it, if not I want to drop it.

I also want to trim everything after the players name, thus leaving an array that looks like this;

[code][0] => Gary Irvin
    [1] => Reuben Railey
    [2] => Jay Butcher
    [3] => Steve Newton[/code]

Anyone got an easy way to do this?

Thanks in advance for your help.
Link to comment
https://forums.phpfreaks.com/topic/27963-array-manipulation-removing-and-trimming/
Share on other sites

you will need to use the array_walk function and creata your own function that uses str_replace or similar..

so something like...
[code]function stripStuff (&$element, $key)
{
if (preg_match('/season/', $element))
  $element = preg_replace('/ \((.)*+season(s)?', $element);
else
  unset($element);
}

array_walk($data,'stripStuff');[/code]

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.