我正在浏览旧页面,其中包含超过 10,000 条评论,我正尝试将其导入到 WordPress。
我正在使用 simple_html_dom.php 库,在本例中这并不重要。
我正在做的是获取一个包含 24 个第一篇帖子的 URL,并获取带有评论的元素。
$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);
}
目前一切都很好,我已经在数组中收到了所有评论。 问题是评论文本、日期和作者位于
项中,没有任何类或 ID,所以我没有触发器单独获取它们,所以我的数组是
[0] => 文本,[1] => 日期和作者,[3] => 文本,[4] => 日期和作者等
我正在尝试将其放入一个新数组中,例如 [text] => 文本,[sign] => 日期和作者:
$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
我得到了双项,所以我的数组变大了一倍,所有内容都翻倍了。
为什么我需要在新数组中使用它?因为我想把它存入数据库。
所以目前我不知道是什么原因导致这个问题。
您将获得数组两次,因为您使用 if 条件在奇数和偶数期间将整个数组值
$commItem
添加到 $commArr
。这就是为什么你的数组时间会加倍。
更换您的代码
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[] = array('text'=>$commItem[0], 'sign'=>$commItem[1]);
}
我认为这可能对你有用:)。
我不是 wp 编码员,多年来我一直用它来进行演示!你可以像这样使用 key,至少我会在 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 ) );
以及如何放入数据库
$stmt = $pdo->prepare("INSERT INTO comments ( " . implode(', ',array_keys($a)) . ") VALUES (" . implode(', ',array_keys($Collected_array_result)) . ")");
$result = $stmt->execute($Collected_array_result);
从数组中获取名称并使用名称创建一个新数组:
$first_name = array_column($a, 'first_name', 'id');
print_r($first_name);
输出
Array ( [5698] => Peter [4767] => Ben [3809] => Joe );
更新:关于sql注入的@Dharman评论并使用准备好的语句在数据库中插入数据,没有要求插入有问题的查询,但如果您使用该查询,请从数组中过滤值或使用如下所示。
$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。