0

I've been searching and searching and can't find anything that works, but this is what I want to do.

This code:

try{
$timeout = 2;
$scraper = new udptscraper($timeout);
$ret = $scraper->scrape('udp://tracker.openbittorrent.com:80',array('0D7EA7F06E07F56780D733F18F46DDBB826DCB65'));
print_r($ret);
}catch(ScraperException $e){
echo('Error: ' . $e->getMessage() . "<br />\n");
echo('Connection error: ' . ($e->isConnectionError() ? 'yes' : 'no') . "<br />\n");
}

Outputs this:

Array ( [0D7EA7F06E07F56780D733F18F46DDBB826DCB65] => Array ( [seeders] => 148 [completed] => 10 [leechers] => 20 [infohash] => 0D7EA7F06E07F56780D733F18F46DDBB826DCB65 ) ) 

And I want that seeder count into a string such as $seeds. How would I go about doing this?

1
  • 1
    Sounds like you want extract. Commented Dec 30, 2012 at 22:34

3 Answers 3

2

Something like this?

$seeds = $ret['0D7EA7F06E07F56780D733F18F46DDBB826DCB65']['seeders'];
Sign up to request clarification or add additional context in comments.

Comments

0

you can user strval() to convert a number to a string.

$string = strval($number);

or you can cast it into a string:

$string = (string)$number;

in your context that would be:

$string = strval($ret['0D7EA7F06E07F56780D733F18F46DDBB826DCB65']['seeders']);

However that odd string is also the first index of the array so you could also do it like this:

$string = strval($ret[0]['seeders']);

or if you want ot use only indexes ('seeders' is also the first index of the second array):

$string = strval($ret[0][0]);

if you just want the number then it's easy too:

$num = $ret[0][0];

Comments

0

It's not clear if you're looking to assign the array value(s?) as a separate variable(s?) or just to cast it into a string. Here's a nice way to accomplish all the above options, by assigning each array key as a separate variable with the matching array value:

$ret_vars = array_pop($ret);
foreach ($ret_vars as $variable_name=>$variable_value) :
    ${$variable_name} = (string)$variable_value;
endforeach;

In your original example, this would end up populating $seeders, $completed, $leechers and $infohash with their matching string values. Of course, make sure these variable names are not used/needed elsewhere in the code. If that's the case, simply add some sort of unique prefix to the ${} construct, like ${'ret_'.$variable_name}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.