cwal
cwal

Reputation: 3872

how do i reverse the order when echoing my results?

i have this bit of code:

<?php   
$file = file_get_contents('http://example.com');
preg_match_all("/<a href=(.*?links.*?)>.*?<\/a>/i", $file, $a);
$count = count($a[1]);
for ($row = 0; $row < $count ; $row++) {
$linkurls = $a[1]["$row"];
echo ' '.$linkurls.' <br>';
}
?> 

and currently it echos the links in order they appear on the website. I would like for it to echo the results in a reverse order (the last link on example.com to echo as the first with this code)

any help is appreciated! thanks.

Upvotes: 1

Views: 1211

Answers (3)

Alin P.
Alin P.

Reputation: 44346

PHP has a function for it: array_reverse.

$a[1] = array_reverse($a[1]);

Also why would you use ["$row"] instead of [$row]? There is no functional difference as numeric strings get converted back to numbers when using them as indexes, so don't worry about that, but just because something can be done doesn't mean you should do it.

Upvotes: 2

Matt Asbury
Matt Asbury

Reputation: 5662

for ($row = $count - 1; $row > -1 ; $row--) {
    $linkurls = $a[1]["$row"];
    echo ' '.$linkurls.' <br>';
}

Upvotes: 2

trickwallett
trickwallett

Reputation: 2468

<?php   
$file = file_get_contents('http://example.com');
preg_match_all("/<a href=(.*?links.*?)>.*?<\/a>/i", $file, $a);
$a = array_reverse($a);
$count = count($a[1]);
for ($row = 0; $row < $count ; $row++) {
$linkurls = $a[1]["$row"];
echo ' '.$linkurls.' <br>';
}
?> 

Upvotes: 1

Related Questions