I'm crawling through the old page with more than 10 000 comments which I'm trying to import to WordPress.
I'm using simple_html_dom.php library, which in this case is not important.
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);
}
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] => text, [1] => date and author, [3] => text, [4] => date and author etc
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++;
}
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.
['text']or['sign']in your array like$values[1]; $values[2];.echo 'Numer wpisu: '.$z.'<br />'; $z++;. so they are not double they the values in your array with same request you ask to be displayed.