将String转换回PHP数组

时间:2017-06-12 12:27:23

标签: 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 />';
}

?>

在第一部分中,我将数组转换为String以获得可读结果,然后在第二部分中,我需要将该字符串转换回单个数组以生成结果。

2 个答案:

答案 0 :(得分: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 />';
}

答案 1 :(得分:0)

由于你的字符串看起来像这样(来自评论):"udp://tracker4.piratux.com:6969/announce","udp://tracker.trackerfix.com:80/announce"你可以使用PHP的explode()函数,如下所示:

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