Foreach循环仅保存最后的数组值,但是var_dump显示不同的结果

问题描述

我正在尝试使用foreach从循环中保存最后一个数组

根据每个$ keyword的逻辑,只要$ threadcount不满足$ maxthread,它将设置所有需要的变量,然后调用$ google-> getbrowser,然后将数组变量从$ google-> getbrowser保存到$ bparr >

变量$ bparr [$ keyword]仅显示$ google-> getbrowser()中的最后一个数组变量,而var_dump($ google-> getbrowser)正确显示

如何将每个$ google-> getbrowser结果添加到$ bparr中,而不仅仅是最后一个结果?

if ($multi)
{
    $threadcount = 0;
    $e = 0;
    $i = 0;
    $page = 1;
    $proxy = array();
    $browser = array();
    $uafilearr = array();
    $cfarr = array();
    $browser = $google->getbrowser();
    foreach ($keywords as $keyword)
    {
        // setup browser option as array
        if($threadcount < $maxthreads)
        {
            if (!empty($pfile))
            {
                $proxy[$keyword] = proxySetup($pfile);
                // explode the proxy to find out if there is username and password
                // usually there is 4 array_count if there is username and password
                $exproxy = explode(":",$proxy[$keyword]);
            
                if (count($exproxy) == 2)
                {
                    $proxyip = $exproxy[0].":".$exproxy[1];
                    $browser->setProxy($proxyip);
                    $browser->setProxyAuth('');
                }
    
                if (count($exproxy) == 4)
                {
                    $proxyip = $exproxy[0].":".$exproxy[1];
                    $proxyauth = $exproxy[2].":".$exproxy[3];
                    $browser->setProxy($proxyip);
                    $browser->setProxyAuth($proxyauth);
                }
            }
            if (!empty($cookiesdir))
            {
                clearstatcache();
                $uafile = $cookiesdir.'\gtu-'.md5($proxy[$keyword]).'.txt';
                $uafilearr[$keyword] = $uafile;
                if (file_exists($uafile)) {
                    $useragent = file_get_contents($uafile);                    
                }
                else
                {
                    touch($uafile);
                    if (!empty($agentfile))
                    {
                        $useragent = agent($agentfile);
                    }
                    else
                    {
                        // you can change the useragent defined here
                        $useragent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/84.0.4147.105 Safari/537.36";
                    }
                    file_put_contents($uafile,$useragent);

                }
                $browser->setUserAgent($useragent);
        
                $cookiesfile = $cookiesdir.'\gtc-'.md5($proxy[$keyword]).'.txt';
                $cfarr[$keyword] = $cookiesfile;
                if (!file_exists($cookiesfile)) {               
                    touch($cookiesfile);
                }
                $browser->setCookieFile($cookiesfile);
            }
            var_dump($google->getbrowser());
            if( !isset($bparr[$keyword])) $bparr[$keyword] = array();
            $bparr[$keyword] = $google->getbrowser();
        }
        else
        {
            break 1;
        }
        $threadcount++;
        
    }
    echo "proxy array\n";
    var_dump($proxy);
    echo "\n";
    echo "useragent array\n";
    var_dump($uafilearr);
    echo "\n";
    echo "cookies array\n";
    var_dump($cfarr);
    echo "\n";
    echo "\n";
    echo "browser array\n";
    var_dump($bparr);
    echo "\n";
}

下面是var_dump($ google-> getbrowser());

的结果
object(SerpScraper\browser)#2 (3) {
  ["headers":protected]=>
  array(7) {
    ["Accept"]=>
    string(124) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signe
d-exchange;v=b3;q=0.9"
    ["Accept-Encoding"]=>
    string(13) "gzip,deflate"
    ["Accept-Charset"]=>
    string(30) "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
    ["Accept-Language"]=>
    string(26) "en-US,en;q=0.9;q=0.8;q=0.7"
    ["Connection"]=>
    string(10) "keep-alive"
    ["Keep-Alive"]=>
    string(3) "115"
    ["User-Agent"]=>
    string(90) "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"
  }
  ["options":protected]=>
  array(14) {
    [10102]=>
    string(0) ""
    [81]=>
    int(0)
    [64]=>
    int(0)
    [58]=>
    int(1)
    [78]=>
    int(10)
    [13]=>
    int(15)
    [10065]=>
    string(27) "D:\htdocs\cacert\cacert.pem"
    [10097]=>
    string(16) "D:\htdocs\cacert"
    [113]=>
    int(1)
    [10004]=>
    string(19) "xxx.xxx.xxx.xxx:5050"
    [10006]=>
    string(32) ""
    [10018]=>
    string(90) "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"
    [10082]=>
    string(62) "D:\htdocs\cookiefiles\gtc-6a38af9410721abf3002d46cbb04cf76.txt"
    [10031]=>
    string(62) "D:\htdocs\cookiefiles\gtc-6a38af9410721abf3002d46cbb04cf76.txt"
  }
  ["cookie_file":protected]=>
  string(62) "D:\htdocs\cookiefiles\gtc-6a38af9410721abf3002d46cbb04cf76.txt"
}
object(SerpScraper\browser)#2 (3) {
  ["headers":protected]=>
  array(7) {
    ["Accept"]=>
    string(124) "text/html,en;q=0.9;q=0.8;q=0.7"
    ["Connection"]=>
    string(10) "keep-alive"
    ["Keep-Alive"]=>
    string(3) "115"
    ["User-Agent"]=>
    string(70) "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0)"
  }
  ["options":protected]=>
  array(14) {
    [10102]=>
    string(0) ""
    [81]=>
    int(0)
    [64]=>
    int(0)
    [58]=>
    int(1)
    [78]=>
    int(10)
    [13]=>
    int(15)
    [10065]=>
    string(27) "D:\htdocs\cacert\cacert.pem"
    [10097]=>
    string(16) "D:\htdocs\cacert"
    [113]=>
    int(1)
    [10004]=>
    string(19) "xxx.xxx.xxx.xxx:5050"
    [10006]=>
    string(32) ""
    [10018]=>
    string(70) "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0)"
    [10082]=>
    string(62) "D:\htdocs\cookiefiles\gtc-f972ff94b3dd45909c948e28ca1f75b2.txt"
    [10031]=>
    string(62) "D:\htdocs\cookiefiles\gtc-f972ff94b3dd45909c948e28ca1f75b2.txt"
  }
  ["cookie_file":protected]=>
  string(62) "D:\htdocs\cookiefiles\gtc-f972ff94b3dd45909c948e28ca1f75b2.txt"
}
object(SerpScraper\browser)#2 (3) {
  ["headers":protected]=>
  array(7) {
    ["Accept"]=>
    string(124) "text/html,en;q=0.9;q=0.8;q=0.7"
    ["Connection"]=>
    string(10) "keep-alive"
    ["Keep-Alive"]=>
    string(3) "115"
    ["User-Agent"]=>
    string(65) "Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0"
  }
  ["options":protected]=>
  array(14) {
    [10102]=>
    string(0) ""
    [81]=>
    int(0)
    [64]=>
    int(0)
    [58]=>
    int(1)
    [78]=>
    int(10)
    [13]=>
    int(15)
    [10065]=>
    string(27) "D:\htdocs\cacert\cacert.pem"
    [10097]=>
    string(16) "D:\htdocs\cacert"
    [113]=>
    int(1)
    [10004]=>
    string(19) "xxx.xxx.xxx.xxx:5050"
    [10006]=>
    string(32) ""
    [10018]=>
    string(65) "Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0"
    [10082]=>
    string(62) "D:\htdocs\cookiefiles\gtc-80e3021c92debe638f494be1db37cdf5.txt"
    [10031]=>
    string(62) "D:\htdocs\cookiefiles\gtc-80e3021c92debe638f494be1db37cdf5.txt"
  }
  ["cookie_file":protected]=>
  string(62) "D:\htdocs\cookiefiles\gtc-80e3021c92debe638f494be1db37cdf5.txt"
}
object(SerpScraper\browser)#2 (3) {
  ["headers":protected]=>
  array(7) {
    ["Accept"]=>
    string(124) "text/html,en;q=0.9;q=0.8;q=0.7"
    ["Connection"]=>
    string(10) "keep-alive"
    ["Keep-Alive"]=>
    string(3) "115"
    ["User-Agent"]=>
    string(109) "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/41.0.2272.101 Safari/
537.36"
  }
  ["options":protected]=>
  array(14) {
    [10102]=>
    string(0) ""
    [81]=>
    int(0)
    [64]=>
    int(0)
    [58]=>
    int(1)
    [78]=>
    int(10)
    [13]=>
    int(15)
    [10065]=>
    string(27) "D:\htdocs\cacert\cacert.pem"
    [10097]=>
    string(16) "D:\htdocs\cacert"
    [113]=>
    int(1)
    [10004]=>
    string(19) "xxx.xxx.xxx.xxx:5050"
    [10006]=>
    string(32) ""
    [10018]=>
    string(109) "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/41.0.2272.101 Safari/
537.36"
    [10082]=>
    string(62) "D:\htdocs\cookiefiles\gtc-e2fc30330ea96d286d0854838bcf18ea.txt"
    [10031]=>
    string(62) "D:\htdocs\cookiefiles\gtc-e2fc30330ea96d286d0854838bcf18ea.txt"
  }
  ["cookie_file":protected]=>
  string(62) "D:\htdocs\cookiefiles\gtc-e2fc30330ea96d286d0854838bcf18ea.txt"
}
object(SerpScraper\browser)#2 (3) {
  ["headers":protected]=>
  array(7) {
    ["Accept"]=>
    string(124) "text/html,en;q=0.9;q=0.8;q=0.7"
    ["Connection"]=>
    string(10) "keep-alive"
    ["Keep-Alive"]=>
    string(3) "115"
    ["User-Agent"]=>
    string(151) "Mozilla/5.0 (Linux; U; Android 4.0.3; en-gb; KFTT Build/IML74K) AppleWebKit/537.36 (KHTML,like Gecko)
Silk/3.68 like Chrome/39.0.2171.93 Safari/537.36"
  }
  ["options":protected]=>
  array(14) {
    [10102]=>
    string(0) ""
    [81]=>
    int(0)
    [64]=>
    int(0)
    [58]=>
    int(1)
    [78]=>
    int(10)
    [13]=>
    int(15)
    [10065]=>
    string(27) "D:\htdocs\cacert\cacert.pem"
    [10097]=>
    string(16) "D:\htdocs\cacert"
    [113]=>
    int(1)
    [10004]=>
    string(19) "xxx.xxx.xxx.xxx:5050"
    [10006]=>
    string(32) ""
    [10018]=>
    string(151) "Mozilla/5.0 (Linux; U; Android 4.0.3; en-gb; KFTT Build/IML74K) AppleWebKit/537.36 (KHTML,like Gecko)
Silk/3.68 like Chrome/39.0.2171.93 Safari/537.36"
    [10082]=>
    string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
    [10031]=>
    string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
  }
  ["cookie_file":protected]=>
  string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
}

下面是var_dump($ bparr);

的结果
browser array
array(5) {
  ["hosting terbaik"]=>
  object(SerpScraper\browser)#2 (3) {
    ["headers":protected]=>
    array(7) {
      ["Accept"]=>
      string(124) "text/html,application/sig
ned-exchange;v=b3;q=0.9"
      ["Accept-Encoding"]=>
      string(13) "gzip,deflate"
      ["Accept-Charset"]=>
      string(30) "ISO-8859-1,*;q=0.7"
      ["Accept-Language"]=>
      string(26) "en-US,en;q=0.9;q=0.8;q=0.7"
      ["Connection"]=>
      string(10) "keep-alive"
      ["Keep-Alive"]=>
      string(3) "115"
      ["User-Agent"]=>
      string(151) "Mozilla/5.0 (Linux; U; Android 4.0.3; en-gb; KFTT Build/IML74K) AppleWebKit/537.36 (KHTML,like Gecko
) Silk/3.68 like Chrome/39.0.2171.93 Safari/537.36"
    }
    ["options":protected]=>
    array(14) {
      [10102]=>
      string(0) ""
      [81]=>
      int(0)
      [64]=>
      int(0)
      [58]=>
      int(1)
      [78]=>
      int(10)
      [13]=>
      int(15)
      [10065]=>
      string(27) "D:\htdocs\cacert\cacert.pem"
      [10097]=>
      string(16) "D:\htdocs\cacert"
      [113]=>
      int(1)
      [10004]=>
      string(19) "xxx.xxx.xxx.xxx:5050"
      [10006]=>
      string(32) ""
      [10018]=>
      string(151) "Mozilla/5.0 (Linux; U; Android 4.0.3; en-gb; KFTT Build/IML74K) AppleWebKit/537.36 (KHTML,like Gecko
) Silk/3.68 like Chrome/39.0.2171.93 Safari/537.36"
      [10082]=>
      string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
      [10031]=>
      string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
    }
    ["cookie_file":protected]=>
    string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
  }
  ["hosting indonesia"]=>
  object(SerpScraper\browser)#2 (3) {
    ["headers":protected]=>
    array(7) {
      ["Accept"]=>
      string(124) "text/html,like Gecko
) Silk/3.68 like Chrome/39.0.2171.93 Safari/537.36"
      [10082]=>
      string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
      [10031]=>
      string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
    }
    ["cookie_file":protected]=>
    string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
  }
  ["vps indonesia"]=>
  object(SerpScraper\browser)#2 (3) {
    ["headers":protected]=>
    array(7) {
      ["Accept"]=>
      string(124) "text/html,like Gecko
) Silk/3.68 like Chrome/39.0.2171.93 Safari/537.36"
      [10082]=>
      string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
      [10031]=>
      string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
    }
    ["cookie_file":protected]=>
    string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
  }
  ["vps murah"]=>
  object(SerpScraper\browser)#2 (3) {
    ["headers":protected]=>
    array(7) {
      ["Accept"]=>
      string(124) "text/html,like Gecko
) Silk/3.68 like Chrome/39.0.2171.93 Safari/537.36"
      [10082]=>
      string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
      [10031]=>
      string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
    }
    ["cookie_file":protected]=>
    string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
  }
  ["hosting cloud"]=>
  object(SerpScraper\browser)#2 (3) {
    ["headers":protected]=>
    array(7) {
      ["Accept"]=>
      string(124) "text/html,like Gecko
) Silk/3.68 like Chrome/39.0.2171.93 Safari/537.36"
      [10082]=>
      string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
      [10031]=>
      string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
    }
    ["cookie_file":protected]=>
    string(62) "D:\htdocs\cookiefiles\gtc-81e37ba09eba2972fc4e695781b0f461.txt"
  }
}

解决方法

如果查看Browser类源代码here,您会发现getBrowser()方法未返回Class的新实例,而是返回了对它是自己的财产,其中包含另一个Object

public function getBrowser()
{
    return $this->browser;
}

因此,每次将$google->getBrowser()添加到数组时,实际上就添加了对同一对象的引用。因此,您在这里有许多对同一Object的引用。

当您在脚本末尾调用var_dump时-它显示了相同对象的许多输出(很明显是相同的)。

请看官方Objects and references文章,以更好地理解引用的工作原理。

解决方案:

要解决您的情况,您只需在每次需要使用Objectclone保存到阵列时就克隆$bparr[$keyword][] = clone $google->getBrowser();

{{1}}