将其他网页中的特定数据分配给变量或数组


Assigning specific data from another webpage to variables or an array

我正试图从一个网页(我不拥有)中获取数据,然后处理该数据。要做到这一点,我需要将它分配给一个数组,或者将它写入MySQL数据库或其他什么。我希望第2列、第4列和第6列能够保存,以便使用它们。以下是我到目前为止的代码,我完全不知道如何操作数据。我认为这与爆炸有关,但我没能做到:

<?php
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'URL');
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);

$dom = new DOMDocument;
@$dom->loadHTML( $content );
//get all td
$items = $dom->getElementsByTagName('td'); 
//display all text
for ($i = 0; $i < $items->length; $i++)
echo $items->item($i)->nodeValue . "<br/>"; 
//below doesn't work
$cells = explode(" ", $dom->getElementsByTagName('td'));
echo $cells;    
?>

$dom->getElementsByTagName('td');将返回DOMNodeList数据类型,而不是array,因此,我想,对其执行explode操作是行不通的。

顺便说一句,当你已经在使用for循环td时,你想通过爆炸做什么?看起来很相似。

代码

<?php
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'URL');
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);

$dom = new DOMDocument;
@$dom->loadHTML( $content );
//get all td
$items = $dom->getElementsByTagName('td'); 
// save the 2nd, 4th and 6th column values
$columnsToSave = array( 2, 4, 6 );
$outputArray = array();
for ( $i = 0; $i < $items->length; $i++ ) {
  $key = $i + 1;
  if( in_array( $key, $columnsToSave ) ) {
     $outputArray[ $key ] = $items->item($i)->nodeValue . "<br/>";
  }
}
print_r( $outputArray );
?>