This is the function:
function NewsDat($url="http://www.url.com/dat/news.dat", $max=5){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curlresult = curl_exec($ch);
$posts = explode("\n", $curlresult); //chop it up by carriage return
unset($curlresult);
$num=0;
$result=array();
foreach($posts as $post){
$result[] = explode("::", $post);
$num++;
if($num>$max-1){
break;
}
}
return $result;
}
var_dump(NewsDat());
Which returns:
array(5) { [0]=> array(14) { [0]=> string(10) "1183443434" [1]=> string(1) "R" [2]=> string(46) "Here is some text"...
I need to echo: 1183443434 and Here is some text...
Can anyone help?
asked Feb 11, 2010 at 13:25
CLiown
13.9k51 gold badges129 silver badges206 bronze badges
-
Anyone know how I can sort the array to get the last items in the DAT file?CLiown– CLiown2010年02月11日 14:04:13 +00:00Commented Feb 11, 2010 at 14:04
2 Answers 2
Basic array handling?
$result = NewsDat();
echo $result[0][0]; //holds "1183443434"
echo $result[0][2]; //holds "Here is some text"
But I don't know if the values are always at this positions when you run your function.
answered Feb 11, 2010 at 13:28
Felix Kling
820k181 gold badges1.1k silver badges1.2k bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Well as NewsDat return an array of arrays, if you need this two fields on each lines, this should do the trick:
$news = NewsDat();
foreach($news as $single_new)
{
echo $single_new[0] . " - " . $single_new[2] . "\n";
}
If you only need these two fields, just:
$news = NewsDat();
$field1 = $news[0][0];
$field2 = $news[0][2];
echo $field1 . " - " . $field2 . "\n";
Comments
lang-php