1

I have the following code:

$urls = file_get_contents('https://www.google.com/#q=test');

preg_match_all('/\b(?:(?:https?|http):\/\/|www\.)[-a-z]*.com/i', $urls, $content);

$i = 10;

while ( $i <= 50 ) {
$i+= 10;

$urls2 = file_get_contents('https://www.google.com/#q=test&start=".$i."'); // pagination Google search Results

preg_match_all('/\b(?:(?:https?|http):\/\/|www\.)[-a-z]*.com/i', $urls2, $contentLoop);

$totalArray = array_push($content,$contentLoop);


}

print_r($totalArray);

This only print number 6

In the while, how do I add several arrays within a single array?

I tried to use the function array_push, but got no success so far

Gislef
  • 1,555
  • 3
  • 14
  • 37

3 Answers3

0

Array_push is used to push only one element to the end of array. You can use here one of two possible solutions (both would save your data into $content array):

  1. Use array_merge.

    array_merge($content,$contentLoop);
    
  2. Loop through $contentLoop.

    foreach($contentLoop as $item){
        array_push($content,$item);
    }
    
0

If you want to combine two arrays in a single one then you can use array_merge — Merge one or more arrays.

e.g.

<?php
$totalArray = array_merge($content,$contentLoop);
print_r($totalArray);
?>

Check official documentation here:

array_merge — Merge one or more arrays

pradeep1991singh
  • 8,185
  • 4
  • 21
  • 31
0

It seems you are trying to scrape Google search results. Scraping is against the Google terms of service. Google had a web search api but it was discontinued in 2014. Google now provides a custom search api. What are the alternatives now that the Google web search API has been deprecated?

Community
  • 1
  • 1
Nadir Latif
  • 3,690
  • 1
  • 15
  • 24