PHP函数帮助

时间:2010-02-11 13:25:31

标签: php

这是功能:

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());

返回:

array(5) { [0]=>  array(14) { [0]=>  string(10) "1183443434" [1]=>  string(1) "R" [2]=>  string(46) "Here is some text"...

我需要回应:1183443434Here is some text...

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

基本array处理?

$result = NewsDat();
echo $result[0][0]; //holds "1183443434"
echo $result[0][2]; //holds "Here is some text"

但是我不知道当你运行你的函数时值是否总是在这个位置。

答案 1 :(得分:1)

当NewsDat返回一个数组数组时,如果你在每一行都需要这两个字段,这应该可以解决问题:

$news = NewsDat();

foreach($news as $single_new)
{
    echo $single_new[0] . " - " . $single_new[2] . "\n";
}

如果您只需要这两个字段,只需:

$news = NewsDat();
$field1 = $news[0][0];
$field2 = $news[0][2];
echo $field1 . " - " . $field2 . "\n";