Jump to content

Archived

This topic is now archived and is closed to further replies.

php_b34st

Arrays???

Recommended Posts

Hi I have the following code which grabs a table from a website

[code]<?php

$user = 'devil may6';

function RSstats($url)
{
$rssstring = file_get_contents($url);
preg_match_all('#<table cellpadding="3" cellspacing="0" border=0>(.*?)</table>#s', $rssstring, $stat);

$score = $stat[1][0];
$score = strip_tags($score);
echo $score;

}

RSstats('http://hiscore.runescape.com/lang/en/aff/runescape/hiscorepersonal.ws?user1=' . $user);

?>


[/code]

and displays it like this

[quote]Personal scores for Devil May6 Skill RankLevelXP Overall 999,998 667 1,597,232 Attack Not Ranked Defence 529,783 57 214,857 Strength Not Ranked Hitpoints 932,293 55 172,561 Ranged Not Ranked Prayer 325,757 46 68,243 Magic 281,017 62 349,834 Cooking Not Ranked Woodcutting 432,602 65 465,040 Fletching 565,357 38 30,988 Fishing Not Ranked Firemaking Not Ranked Crafting Not Ranked Smithing Not Ranked Mining Not Ranked Herblore 219,323 34 21,909 Agility Not Ranked Thieving Not Ranked Slayer Not Ranked Farming Not Ranked Runecraft Not Ranked Construction Not Ranked[/quote]

I would like this information put into an array like this:

[quote]Array
(
    [0] => Array
        (
            [0] => Overall
            [1] => 999,998
            [2] => 667
            [3] => 1,597,232
        )

    [1] => Array
        (
            [0] => Attack
            [1] => Not Ranked
        )

    [2] => Array
        (
            [0] => Defence
            [1] => 529,783
            [2] => 57
            [3] => 214,857
        )
etc...[/quote]

But I dont know how i would go about this could anyone help please?

Share this post


Link to post
Share on other sites
Well, if we're talking simply putting into an array, the very easiest way to put it all into an array would be:

[code]<?php

$score = explode(" ", $score);

?>[/code]

You can then work with that array at that point. Of course you can get much more detailed.

Share this post


Link to post
Share on other sites
How do i use it once it is in an array? i tried:

[code]echo $score[0];
echo $score[1];[/code]

but nothing seemed to happen?

Share this post


Link to post
Share on other sites
Try this

[code]<?php

$txt = "Personal scores for Devil May6 Skill RankLevelXP Overall 999,998 667 1,597,232 Attack Not Ranked Defence 529,783 57 214,857 Strength Not Ranked Hitpoints 932,293 55 172,561 Ranged Not Ranked Prayer 325,757 46 68,243 Magic 281,017 62 349,834 Cooking Not Ranked Woodcutting 432,602 65 465,040 Fletching 565,357 38 30,988 Fishing Not Ranked Firemaking Not Ranked Crafting Not Ranked Smithing Not Ranked Mining Not Ranked Herblore 219,323 34 21,909 Agility Not Ranked Thieving Not Ranked Slayer Not Ranked Farming Not Ranked Runecraft Not Ranked Construction Not Ranked";

$cats = array('Overall', 'Attack', 'Defence', 'Strength', 'Hitpoints',
'Ranged', 'Prayer', 'Magic', 'Cooking', 'Woodcutting', 
'Fletching', 'Fishing', 'Firemaking', 'Crafting', 'Smithing',
'Mining', 'Herblore', 'Agility', 'Thieving', 'Slayer',
'Farming', 'Runecraft', 'Construction');
$score = array();
$data = explode (' ', $txt);
$key = 'header';
foreach ($data as $word) {
    if (in_array($word, $cats)) {
    $score[$word] = array();
    $key = $word;
    }
    elseif ($word=='Not') {
    $score[$key][] = 'Not ranked';
    }
    elseif ($word=='Ranked') {
    continue;
    }
    else $score[$key][] = $word;
}

// view the results

echo '<pre>', print_r($score, true), '</pre>';
?>[/code]

Share this post


Link to post
Share on other sites
Yes that is how i wanted the data splitting up but the problem there is you have manually defined the txt to be put into an array, that txt will change and thats y i need to get it using file(). is there a way to do that after getting the data from the website rather than manually defining $txt? i tried changing $txt so to $txt = $score; but that messed it up.

Share this post


Link to post
Share on other sites
Of course I manually defined text. I had to have something to process so I used your quoted output.

Substitute the text you get from the site.

Share this post


Link to post
Share on other sites
try this

[code]
$user = 'devil may6';

function RSstats($url)
{
$rssstring = file_get_contents($url);
preg_match_all('#<table cellpadding="3" cellspacing="0" border=0>(.*?)</table>#s', $rssstring, $stat);

$score = $stat[1][0];
$score = strip_tags($score);
return $score;

}

$txt = RSstats('http://hiscore.runescape.com/lang/en/aff/runescape/hiscorepersonal.ws?user1=' . $user);
[/code]

Share this post


Link to post
Share on other sites

×

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.