数组foreach循环PHP [英] Array foreach loop PHP

查看:56
本文介绍了数组foreach循环PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这是我的情况:

Hello everyone here is my case:

我正在浏览旧页面,其中包含超过1万条评论,我正尝试将它们导入WordPress。

I'm crawling through the old page with more than 10 000 comments which I'm trying to import to WordPress.

我正在使用simple_html_dom.php库,在这种情况下这并不重要。

I'm using simple_html_dom.php library, which in this case is not important.

我要做什么m做的是获取一个包含24条第一篇文章的URL,并获得包含评论的元素。

What I'm doing is getting a URL with 24 first posts crawling through them and getting an element with comments.

$url = 'http://xx/aktualnosci,wszystkie,0,'.$x.'.html'; //some URL with first 24 posts
$html = file_get_html($url);

$articlesCount = 0;
$commentsCount = 0;

foreach ($html->find('ul.news_codrugi li') as $article) { //get all 24 posts urls
    $rawLink = $article->find('a');

    foreach ($rawLink as $testLink) {    
        $link = 'http://xx/'.$testLink->href;

        $rawTitle = $testLink->href;
        $rawTitle = explode(",", $rawTitle);
        $ggTitle = $rawTitle[1];
        $htmlNew = file_get_html($link);

        foreach ($htmlNew->find('div.komentarz_lista') as $comment) { //comment element
            $comm = $comment->find('p');
            foreach ($comm as $commText) {
                $cleanerCommText = trim(strip_tags($commText));
                $item['commRaw'] = $cleanerCommText;
                $comments[] = $item;
            }
            $commentsCount++;
        }
        $articlesCount++;
    }
    //unset($articles);
}

此刻一切都很好,我在Array中有所有注释。
问题是注释文本,日期和作者位于

项中,没有任何类或ID,因此我没有触发器来单独获取它们,因此我的数组为

For this moment everything is pretty fine, I've got all comments in Array. The problem is that the comments text, date and author are in

item without any class or ID so I've got no trigger to get them separately, so my array is


[0] =>文本,[1] =>日期和作者,[3] =>文本,[4] =>日期和作者,等等

[0] => text, [1] => date and author, [3] => text, [4] => date and author etc

我正在尝试将其放入新数组中,例如[text] => text,[sign] => date和author:

I'm trying to put it in to a new array like [text] => text, [sign] => date and author :

$x = $commentsCount;
echo $x.'<br />';

$rawComm = array_column($comments, 'commRaw');
$rawCommCount = count($rawComm);

echo 'Pobrane wpisy: '.$rawCommCount.'<br />';
$z = 0;

foreach($rawComm as $commItem) {
    if($z % 2 == 0) {
        $commArr['text']    = $commItem;
    }else{
        $commArr['sign']    = $commItem;
        //echo $commItem;
    }
    echo 'Numer wpisu: '.$z.'<br />';
    $z++;
}

在最后一个循环中 foreach($ rawComm as $ commItem)当我回显所有值时,一切都很好,我已经正确打印了注释文本和注释日期以及作者。但是,当我尝试将其放入新数组 $ commArr 时,我得到的是双项,因此我的数组要大一倍,所有内容都加倍。

In the last loop foreach($rawComm as $commItem) when I echo the values everything is fine, I've got Comment Text and Comment Date and Author printed properly. But when I'm trying to put it into a new array $commArr I'm getting double items, so my array is twice bigger with doubled everything.

为什么我需要一个新数组?因为我想把它放到数据库中。

And why do I need it in a new array? Because I want to put it into a DB.

所以在这一点上,我不知道是什么原因导致了这个问题。有什么帮助吗? :)

So at this point, I don't know what causes this problem. Any help? :)

谢谢

推荐答案

我不是wp编码器多年以来,我一直将其用于演示!您可以使用这样的键,至少要在php中使用。

I am not a wp coder and been years I used it for a demo! You can use key like this, atleast how I would do in php.

    $a = array(
      array(
        'id' => 5698,
        'first_name' => 'Peter',
        'last_name' => 'Griffin',
      ),
      array(
        'id' => 4767,
        'first_name' => 'Ben',
        'last_name' => 'Smith',
      ),
      array(
        'id' => 3809,
        'first_name' => 'Joe',
        'last_name' => 'Doe',
      )
    );
//Collect array values excrated from foreach
    $Collected_array_result = array();
    foreach($a as $key => $value ) {
        $Collected_array_result[':'.$key] = $value;
    }
   //Create another array from that values 
    print_r($Collected_array_result);

输出

Array ( [:0] => Array ( [id] => 5698 [first_name] => Peter [last_name] => Griffin ) [:1] => Array ( [id] => 4767 [first_name] => Ben [last_name] => Smith ) [:2] => Array ( [id] => 3809 [first_name] => Joe [last_name] => Doe ) );

以及如何放入db

$stmt = $pdo->prepare("INSERT INTO comments ( " . implode(', ',array_keys($a)) . ") VALUES (" . implode(', ',array_keys($Collected_array_result)) . ")");
$result = $stmt->execute($Collected_array_result);

从数组中获取名称并创建一个具有以下名称的新数组:

Get names from array and create a new array with names:

$first_name = array_column($a, 'first_name', 'id');
print_r($first_name);

输出

Array ( [5698] => Peter [4767] => Ben [3809] => Joe );

更新:在@Dharman注释中进行sql注入并将数据插入db使用准备好的语句,不要求输入有问题的查询,但是如果您使用该查询,请从数组中过滤值或像下面这样使用。

UPDATE : On @Dharman comment for sql injection and insert data in db using prepared statement, wasnt asked for insert query in question but in case you use that query, please filter values from array or use like following.

$first_name = array_column($a, 'first_name');
$first = implode(', ', $first_name);
 echo $first;

$last_names = array_column($a, 'last_name');
$last = implode(', ', $last_names);
 echo $last;

$id = array_column($a, 'id');
$iddd = implode(', ', $id);
 echo $iddd;

$sql = "INSERT INTO comments (first_name, last_names) VALUES (?,?)";
$stmt= $pdo->prepare($sql);
$stmt->execute([$first, $last]);

内插数组中的所有值,并按1加到查询1。

imploded all values in array and added to query 1 by 1.

这篇关于数组foreach循环PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆