1

我正在使用 simplehtmldom 解析网页并提取数据,然后将其放入 mysql 数据库中。我成功提取数据并将其放入数组中,但现在我面临的问题是如何将此数组转换为变量,以便我可以在我的数据库查询中使用它来将记录插入特定字段。我想将数组转换为单个变量

这是代码

<?php
     include("simple_html_dom.php");

     $html = file_get_html('abc.html'); 

     $sched = array();
     foreach ( $html->find('tr[class=highlight-darkgrey]') as $event ) {
         $item['title'] = trim($event->find('td', 1)->plaintext);

         $sched[] = $item;
    }

    var_dump($sched);
?>

输出是

array (size=6)
 0 => 
 array (size=1)
  'title' => string 'Network admin, field engineer, engineering analyst, sales  executive, PA to HR director Required by Telecommunication Company' (length=124)
 1 => 
 array (size=1)
  'title' => string 'Karachi, Hydrabad, Lahore, Rawalpindi, Peshawar, Multan, Faisalabad' (length=67)
 2 => 
 array (size=1)
  'title' => string '5 - 6 Years' (length=11)
 3 => 
 array (size=1)
  'title' => string 'Knowledge of Field' (length=18)
 4 => 
 array (size=1)
  'title' => string '' (length=0)
 5 => 
 array (size=1)
  'title' => string '- Salary and incentives are not full and final. Can be discussed on final interview.

可以请有人帮助我实现它。提前致谢

4

5 回答 5

1

好吧,如果这需要进入特定领域,那么您可以执行以下操作:

INSERT INTO table_name (column1, column2, column3,...)
VALUES ($sched[0]['title'], $sched[1]['title'], $sched[2]['title'],...);
于 2013-07-17T15:15:42.413 回答
0

php有获取单个变量的功能, http: extract() //php.net/manual/en/function.extract.php

但是我不知道你为什么要这样做,而不是仅仅使用一个循环来遍历你的数组。

于 2013-07-17T15:08:14.003 回答
0

为什么不直接使用数组值?对我来说,构建一个调用每个字段的子数组也没有任何意义title

$sched = array();
foreach ( $html->find('tr[class=highlight-darkgrey]') as $event ) {
    $sched[] = trim($event->find('td', 1)->plaintext);
}

然后访问以下值:

$value0 = $sched[0];
$value1 = $sched[1];

// PDO example
$sth = $dbh->prepare(
    'INSERT INTO table VALUES (?, ?, ...)'
);
$sth->execute(array($sched[0], $sched[1], ...));
// or if array count is right
$sth->execute($sched);
于 2013-07-17T15:17:38.397 回答
0

你的意思是像

foreach($sched as $item) {
   $title = $item['title'];
   insert_into_db($title);
}

?

于 2013-07-17T15:18:28.133 回答
0

为什么要将数据从一个perfectly good位置(即数组)移动到标量变量中。

无缘无故地使您的内存使用量翻倍。

我假设您想要将标题存储在表格中

这样你就可以 :

foreach ( $sched as $one_sched ) {

    // do your database update using $one_sched['title'] as the data identifier.

}
于 2013-07-17T15:18:32.837 回答