php返回一个数组

时间:2014-12-04 10:36:28

标签: php arrays

我正在从另一个网站上抓取一张桌子,并设法抓取数据并将其作为一个数组返回。

我正在使用for循环数组并打印数据和数据显示。 '行中有三个项目。 -

地址,乐队,价格

我试图将返回的数组分配给这些标题,以便我可以搜索地址并将其与当前地址匹配,但我似乎无法为其找到正确的代码。

include ("simple_html_dom.php");
// Dump contents (without tags) from HTML
$html = file_get_html('http://www.mycounciltax.org.uk/results?postcode=b757ep&search=Search');
$ret = $html->find("td");
$totalret = count($ret);
for ($x = 0; $x <= $totalret; $x++) {
    echo "$ret[$x] $x <br>";
}

这就是代码并返回

90, REDDICAP HEATH ROAD, SUTTON COLDFIELD, WEST MIDLANDS B75 7EP 0 
D 1 
£1294 2 
92, REDDICAP HEATH ROAD, SUTTON COLDFIELD, WEST MIDLANDS B75 7EP 3 
D 4 
£1294 5 
94, REDDICAP HEATH ROAD, SUTTON COLDFIELD, WEST MIDLANDS B75 7EP 6 
D 7 
£1294 8 
96, REDDICAP HEATH ROAD, SUTTON COLDFIELD, WEST MIDLANDS B75 7EP 9 
D 10 
£1294 11

首先你有地址,然后是带(D),然后是下面的价格(每行末尾的数字是$ x仅用于测试目的)

我如何转换该数组,以便为​​每一行(地址,频段,价格)分配正确的标题,以便我可以搜索和匹配数据?

3 个答案:

答案 0 :(得分:0)

这样的东西?

$array = array();
$arrayIndex = 0;
$col = 0;
foreach($ret as $item) {
    $array[$arrayIndex][] = $item;
    $col ++;
    if ($col === 2) {
        $arrayIndex ++;
        $col = 0;
    }
}

答案 1 :(得分:0)

尝试干净的换行符:

$html = str_replace("\n",'',$html);

答案 2 :(得分:0)

以下代码将返回类似

的内容
Array (
   [0] => Array
       (
           [address] => 90, REDDICAP HEATH ROAD, SUTTON COLDFIELD, WEST MIDLANDS B75 7EP
           [band] => D
           [price] => £1294
       )
   [1] => Array
       (
           [address] => 92, REDDICAP HEATH ROAD, SUTTON COLDFIELD, WEST MIDLANDS B75 7EP
           [band] => D
           [price] => £1294
       )


$html = file_get_html('http://www.mycounciltax.org.uk/results?postcode=b757ep&search=Search');
$tds = $html->find("td");

$searchTerm = '92, REDDICAP HEATH ROAD, SUTTON COLDFIELD, WEST MIDLANDS B75 7EP';

$searchResults = array();

$properties = array();
$current = 0;


foreach ($tds as $td) {
    $td = trim(strip_tags(trim($td)));
    // or  $td = trim($td->innertext());
    $properties[(int)($current / 3)][] = $td;
    $current++;
}

$keys = array('address', 'band', 'price');
foreach ($properties as &$property) {
    $property = array_combine($keys, $property);
    if ($property['address'] == $searchTerm) {
        $searchResults[] = $property;
    }
}

print_r($properties);
print_r($searchResults);