2013-07-22 31 views
0

我从其他网站获取数据,删除了我不需要的数据。并希望将我收集的信息存储到文件中。我已经做了我需要的一切,但我的文件写入阵列的每一行从PHP中的XML中将数据逐行保存到文件中

这里是代码我的工作:

$xml = simplexml_load_file('website.xml'); 

    $item_array = array(); 
    $item_array[0] = 0; 
    $item_array[1] = 2; 
    $item_array[2] = 4; 
    $item_array[3] = 6; 
    $item_array[4] = 8; 
    $item_array[5] = 9; 
    $item_array[6] = 10; 
    $item_array[7] = 11; 
    $item_array[8] = 12; 
    $item_array[9] = 13; 
    $item_array[10] = 14; 
    $item_array[11] = 15; 
    $item_array[12] = 16; 

    $patterns = array(); 
    $patterns[0] = '/Sum It Up/'; 
    $patterns[1] = '/=/'; 
    $patterns[2] = '/Powerball/'; 
    $patterns[3] = '/MegaBall/'; 
    $patterns[4] = '/Megaplier/'; 
    $patterns[5] = '/Bonus Ball/'; 

    $print = array(); 
    foreach($item_array as $news){ 
    echo $xml->channel->item[$news]->title.'<br>'; 
    $new_string = $xml->channel->item[$news]->description; 
    $new_string = preg_replace("/[^A-Za-z0-9 ]/", '', $new_string); 
    $new_string = preg_replace($patterns, '', $new_string); 
    echo $new_string.'<br>'; 
    $print[] = array($new_string); 
    } 

    $filename = 'numbers.txt'; 
    $handle = fopen($filename, 'w'); 
    @$output = implode("\r\n", $print); 
    fwrite($handle, $output); 
    fclose($handle); 

我只是想保持这些数字作为阵列和写号码到一个文件中逐行。

回答

0

从下面的讨论,最终解决问题。这里应该使用$print[] = $new_string来确保$print是一个一级数组。然后您可以使用var_export或自定义样式逐行打印。 $print[]表示将项目添加到此数组中,如果项目已经是数组,则$ print将是2级数组。

-----老回答-----

使用serialize()功能,它可以序列化数组的字符串,然后你可以将其写入文件。使用unserialize()可以使字符串回到数组。

我的路
+0

所以哪来我把 – blackfilms

+0

变化** $输出=破灭(” \ r \ n“,$ array); ** to ** $ ouput = serialize($ array); ** – TroyCheng

+0

但现在它正在用'a:13:{i:0; a:1: {i:0; s:21:' – blackfilms

0

,VBS编码VS json_encode是没事的,序列化也是一种选择,如果你不介意的文件大小

相关问题