2017-06-12 130 views
1

我有兩個不同的洪流報廢類,我想合併它們,它們單獨工作正常,但唯一的問題是,當我將字符串轉換爲數組時,它不起作用。將字符串轉換回數組PHP

這就是我目前使用的代碼。

<?php 
require 'scraper.php'; 
require 'tor-info.php'; 

$scraper = new Scrapeer\Scraper(); 
$torrent = new Torrent('./test.torrent'); 

$hashix = $torrent->hash_info(); 
var_dump($torrent->scrape()); // shows trackers list like this array(21) { ["udp://tracker4.piratux.com:6969/announce"]=> bool(false) ["udp://tracker.trackerfix.com:80/announce"]=> bool(false) ["udp://tracker.pomf.se:80/announce"]=> bool(false) ["udp://torrent.gresille.org:80/announce"]=> bool(false) so on.. 

$trackers = array("udp://tracker4.piratux.com:6969/announce", "udp://tracker.trackerfix.com:80/announce", "udp://tracker.pomf.se:80/announce"); // now here we need that trackers list 
$hash = array($hashix); 
$info = $scraper->scrape($hash, $trackers); 

//print_r($info); 

echo '<br><hr>'; 

foreach ($info as $key => $value) { 
echo 'SEEDS :' .$value['seeders'].'<br />'; 
echo 'LEECHES :' .$value['leechers'].'<br />'; 
} 

?> 

在第一部分中我轉換數組字符串以得到可讀的結果,然後在第二部分中,我需要該字符串轉換回單一陣列以產生結果。

+0

重組是什麼樣的? –

+0

我需要像這樣'$ trackers = array(「udp://tracker4.piratux.com:6969/announce」,「udp://tracker.trackerfix.com:80/announce」)' – Aliza

+0

重新組合它顯示這樣的結果'array(0 =>'「udp://tracker4.piratux.com:6969/announce」',1 =>'「udp://tracker.trackerfix.com:80/announce」',2 =>'「udp://tracker.pomf.se:80/announce」')' – Aliza

回答

1

你爲什麼不只是讓他們數組開始?

<?php 
require 'scraper.php'; 
require 'tor-info.php'; 

$scraper = new Scrapeer\Scraper(); 
$torrent = new Torrent('./test.torrent');  
$hashix = $torrent->hash_info(); 
$trackers = array_keys($torrent->scrape()); 

$hash = array($hashix); 
$info = $scraper->scrape($hash, $trackers); 

echo '<br><hr>'; 

foreach ($info as $key => $value) { 
    echo 'SEEDS :' .$value['seeders'].'<br />'; 
    echo 'LEECHES :' .$value['leechers'].'<br />'; 
} 
+0

好吧我已經嘗試過這樣'$ trackerArray = []; foreach($ torrent-> scrape()as $ trackx => $ k){trackerArray [] = $ trackx; } $ tracker ='「'.implode('」,「',$ trackerArray)。'」'; $ trackers = array($ tracker); ($ hash = $ trackers);' 但仍不能正常工作 – Aliza

+0

);'看到更新。 – apokryfos

+0

好吧我已經嘗試過這樣'$ trackerArray = array_keys($ torrent-> scrape()); \t $ tracker ='「'.implode('」,「',$ trackerArray)。'」'; \t $ trackers = array($ tracker); \t \t $ hash = array('40e22987e52a8a90b2513792266905dae84cb29f'); \t $ info = $ scraper-> scrape($ hash,$ trackers);'但仍然不起作用 – Aliza

0

由於您的字符串看起來像這樣(從評論):"udp://tracker4.piratux.com:6969/announce","udp://tracker.trackerfix.com:80/announce"你可以使用PHP的爆炸()函數,就像這樣:

$recombined = explode(',', str_replace('"', '', $trackers)); 
+0

感謝您的友好迴應,但如果我不使用引號圍繞字符串,它不起作用的陣列。 – Aliza