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



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

根据每个$关键字的逻辑,只要$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 arrayn";
var_dump($proxy);
echo "n";
echo "useragent arrayn";
var_dump($uafilearr);
echo "n";
echo "cookies arrayn";
var_dump($cfarr);
echo "n";
echo "n";
echo "browser arrayn";
var_dump($bparr);
echo "n";
}

以下是var_dump($google->getBrowser(((;

object(SerpScraperBrowser)#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:htdocscacertcacert.pem"
[10097]=>
string(16) "D:htdocscacert"
[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:htdocscookiefilesgtc-6a38af9410721abf3002d46cbb04cf76.txt"
[10031]=>
string(62) "D:htdocscookiefilesgtc-6a38af9410721abf3002d46cbb04cf76.txt"
}
["cookie_file":protected]=>
string(62) "D:htdocscookiefilesgtc-6a38af9410721abf3002d46cbb04cf76.txt"
}
object(SerpScraperBrowser)#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(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:htdocscacertcacert.pem"
[10097]=>
string(16) "D:htdocscacert"
[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:htdocscookiefilesgtc-f972ff94b3dd45909c948e28ca1f75b2.txt"
[10031]=>
string(62) "D:htdocscookiefilesgtc-f972ff94b3dd45909c948e28ca1f75b2.txt"
}
["cookie_file":protected]=>
string(62) "D:htdocscookiefilesgtc-f972ff94b3dd45909c948e28ca1f75b2.txt"
}
object(SerpScraperBrowser)#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(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:htdocscacertcacert.pem"
[10097]=>
string(16) "D:htdocscacert"
[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:htdocscookiefilesgtc-80e3021c92debe638f494be1db37cdf5.txt"
[10031]=>
string(62) "D:htdocscookiefilesgtc-80e3021c92debe638f494be1db37cdf5.txt"
}
["cookie_file":protected]=>
string(62) "D:htdocscookiefilesgtc-80e3021c92debe638f494be1db37cdf5.txt"
}
object(SerpScraperBrowser)#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(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:htdocscacertcacert.pem"
[10097]=>
string(16) "D:htdocscacert"
[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:htdocscookiefilesgtc-e2fc30330ea96d286d0854838bcf18ea.txt"
[10031]=>
string(62) "D:htdocscookiefilesgtc-e2fc30330ea96d286d0854838bcf18ea.txt"
}
["cookie_file":protected]=>
string(62) "D:htdocscookiefilesgtc-e2fc30330ea96d286d0854838bcf18ea.txt"
}
object(SerpScraperBrowser)#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(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:htdocscacertcacert.pem"
[10097]=>
string(16) "D:htdocscacert"
[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:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
[10031]=>
string(62) "D:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
}
["cookie_file":protected]=>
string(62) "D:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
}

下面是var_dump($bparr(的结果;

browser array
array(5) {
["hosting terbaik"]=>
object(SerpScraperBrowser)#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/sig
ned-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(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:htdocscacertcacert.pem"
[10097]=>
string(16) "D:htdocscacert"
[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:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
[10031]=>
string(62) "D:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
}
["cookie_file":protected]=>
string(62) "D:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
}
["hosting indonesia"]=>
object(SerpScraperBrowser)#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/sig
ned-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(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:htdocscacertcacert.pem"
[10097]=>
string(16) "D:htdocscacert"
[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:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
[10031]=>
string(62) "D:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
}
["cookie_file":protected]=>
string(62) "D:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
}
["vps indonesia"]=>
object(SerpScraperBrowser)#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/sig
ned-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(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:htdocscacertcacert.pem"
[10097]=>
string(16) "D:htdocscacert"
[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:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
[10031]=>
string(62) "D:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
}
["cookie_file":protected]=>
string(62) "D:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
}
["vps murah"]=>
object(SerpScraperBrowser)#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/sig
ned-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(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:htdocscacertcacert.pem"
[10097]=>
string(16) "D:htdocscacert"
[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:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
[10031]=>
string(62) "D:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
}
["cookie_file":protected]=>
string(62) "D:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
}
["hosting cloud"]=>
object(SerpScraperBrowser)#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/sig
ned-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(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:htdocscacertcacert.pem"
[10097]=>
string(16) "D:htdocscacert"
[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:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
[10031]=>
string(62) "D:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
}
["cookie_file":protected]=>
string(62) "D:htdocscookiefilesgtc-81e37ba09eba2972fc4e695781b0f461.txt"
}
}

如果您查看此处的Browser类源代码,您会发现getBrowser()方法没有返回Class的新实例,而是返回对其自身属性的引用,该属性包含另一个Object:

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

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

当您在脚本结束时调用var_dump时,它会显示同一对象的许多输出(显然是相同的(。

看看官方的对象和参考文献文章,更好地了解参考文献是如何工作的。

解决方案:

要解决这种情况,每次需要使用clone:将Object保存到阵列时,您只需克隆即可

$bparr[$keyword][] = clone $google->getBrowser();

最新更新