Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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_Html_Simple Html Dom - Fatal编程技术网

Php 从表中抓取行,然后抓取单元格

Php 从表中抓取行,然后抓取单元格,php,html,simple-html-dom,Php,Html,Simple Html Dom,我正在使用抓取一张表(图中),然后抓取每个tr。这会将行中的所有项目作为一个字符串输出 $contents = $html->find('div[class=product-table]', 0); $titles = $contents->find('tr'); 我如何创建一个数组,既可以将数据保存在行中,又可以输出单个单元格?我希望能够说一行中有7个单元格(这可能是灵活的),并且能够将行中的每个单元格作为变量输出(我需要用这些做其他事情) //产品代码标题 $content

我正在使用抓取一张表(图中),然后抓取每个
tr
。这会将行中的所有项目作为一个字符串输出

$contents = $html->find('div[class=product-table]', 0);
$titles = $contents->find('tr');

我如何创建一个数组,既可以将数据保存在行中,又可以输出单个单元格?我希望能够说一行中有7个单元格(这可能是灵活的),并且能够将行中的每个单元格作为变量输出(我需要用这些做其他事情)


//产品代码标题
$contents=$html->find('div[class=product table]',0);
$data=array();
$rows=$contents->find('tr');
$counter=1;
foreach($key\u row=>$row形式的行){
//在此处理行
foreach($row->find('td')作为$key\u cell=>$cell){
$field\u key=“field\u 5ae0882f9d6f9”;
$data[]=数组(
“列标题”=>带标签($cell->innertext),
);
$counter++;
//处理当前行迭代中的每个单元格
//或者在每个单元格中需要执行的任何操作(计算等)
//$data[]=然后将其推入数组中(您可以先使用键,然后在“$data”中使用它)
}
} ?>

就像我在评论中所说的那样,对行处理第一个循环,对每行,对每个单元格处理另一个foreach。所以基本上你需要两个

由于您没有示例标记,因此没有太多内容可供讨论,但这里有一个想法:


旁注:请注意,如果您希望跳过标题,只需使用计数器和if条件,然后继续即可。

只需先在每行上循环,然后您就可以决定是否要进一步循环每个单元格(第一级为行,第二级为单元格)@Ghost我想最好将其全部添加到数组中,初始数组是行,然后在每个单元格内。是的,你可以这样做,本质上,你可以将它视为一个多级数组,(你将表作为父数组)每一行代表每一个子级别,然后在每一行中有密钥对值<代码>数组(数组(大小=>3,重量=>99公斤…等等))
谢谢你让它工作,一切都很清楚。关于下一个问题-@Rob我看到了下一个问题,对wp和acf不太熟悉,但是你确定你得到了正确的父字段id,并且你不应该在foreach循环单元完成后放置子字段吗?谢谢你的关注,非常感谢。我让它向子中继器添加行,因此它似乎是正确的字段名称等。我已经将
add_sub_行
移动到循环之外,但现在它只添加了1行,而之前它添加了7行,但没有填充数据。我认为你不必知道WP或ACF,更重要的是获得正确的逻辑。
// Product Code Titles
$contents = $html->find('div[class=product-table]', 0);
$data = array();
$rows = $contents->find('tr');
$counter = 1;
foreach ($rows as $key_row => $row) {
    // process rows here
    foreach ($row->find('td') as $key_cell => $cell) {
      $field_key = "field_5ae0882f9d6f9";
      $data[] = array(
        "column_title" => strip_tags($cell->innertext),
      );
      $counter++;

        // process each cell on the current row iteration
        // or whatever you need to do in each cell (calculations and whatnot)
        // $data[] = then push it inside an array (you can prolly use the keys and use it in `$data`)
    }
} ?>
<pre>
<?php print_r($data); ?>
</pre>
<?php update_field( $field_key, $data, $post_id );
$data = array();
$rows = $contents->find('tr');
foreach ($rows as $key_row => $row) {
    // process rows here
    foreach ($row->find('td') as $key_cell => $cell) {
        // process each cell on the current row iteration
        echo $cell->innertext;
        // or whatever you need to do in each cell (calculations and whatnot)
        // $data[] = then push it inside an array (you can prolly use the keys and use it in `$data`)
    }
}