2010-02-11 166 views
0

這是函數: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...

誰能幫助?

+0

任何人都知道我可以如何排序數組以獲取DAT文件中的最後一項? – CLiown 2010-02-11 14:04:13

回答

1

基本array處理?

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

但我不知道當你運行你的函數時,值是否總是在這個位置。

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";