0

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?

1
  • Anyone know how I can sort the array to get the last items in the DAT file? Commented Feb 11, 2010 at 14:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

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.