Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数组转换为单个变量_Php_Simple Html Dom - Fatal编程技术网

Php 将数组转换为单个变量

Php 将数组转换为单个变量,php,simple-html-dom,Php,Simple Html Dom,我使用simplehtmldom解析网页,提取数据,然后将其放入mysql数据库。我成功地提取了数据并将其放入数组中,但现在我面临的问题是如何将此数组转换为变量,以便在数据库查询中使用它将记录插入特定字段中。我想把数组转换成单个变量 这是密码 <?php include("simple_html_dom.php"); $html = file_get_html('abc.html'); $sched = array(); foreach ( $

我使用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);
?>

请有人帮我实现它。提前感谢

Php有一个获取单个变量的函数,
extract()


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

Php有一个获取单个变量的函数,
extract()


但是,我不知道为什么要这样做,而不只是使用循环遍历数组。

如果需要在特定字段中执行此操作,则可以执行以下操作:

INSERT INTO table_name (column1, column2, column3,...)
VALUES ($sched[0]['title'], $sched[1]['title'], $sched[2]['title'],...);

好吧,如果这需要在特定领域中进行,那么您可以这样做:

INSERT INTO table_name (column1, column2, column3,...)
VALUES ($sched[0]['title'], $sched[1]['title'], $sched[2]['title'],...);

为什么不直接使用数组值呢?同样对我来说,在每个字段都被称为
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);

为什么不直接使用数组值呢?同样对我来说,在每个字段都被称为
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);

你是说什么

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

你是说像这样的东西吗

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

为什么要将数据从一个非常好的位置(即数组)移动到标量变量中

在没有实际原因的情况下,将内存使用率提高一倍

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

因此,您可以:

foreach ( $sched as $one_sched ) {

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

}

为什么要将数据从一个非常好的位置(即数组)移动到标量变量中

在没有实际原因的情况下,将内存使用率提高一倍

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

因此,您可以:

foreach ( $sched as $one_sched ) {

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

}

实际上,您拥有的是一个数组数组。我不明白您的问题是什么,因为您显然已经知道如何使用foreach构造。也许您可以展示一些您尝试过的代码,这些代码并不是您想要的,您实际拥有的是一个数组数组。我不明白您的问题是什么,因为您显然已经知道如何使用foreach构造。也许您可以展示一些您尝试过的代码,这些代码并不是您想要的。“但是我不知道您为什么要这样做,而不仅仅是使用循环遍历数组。”——因为它更简单、更快?@TerryHarvey所有这些都取决于数据库结构。如果每个标题都进入到它自己的数组中,那么与使用循环为您执行查询相比,您为什么要编写一系列不同的查询呢?“然而,我不知道您为什么要这样做,而不仅仅是使用循环遍历数组。”—因为它更简单、更快?@TerryHarvey所有这些都取决于数据库结构。如果每个标题都进入到它自己的数组中,那么与使用循环为您执行查询相比,您为什么要写出一堆不同的查询?谢谢Sumoa,这就是我要找的。谢谢Sumoa,这就是我要找的。