Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在多维foreach()循环中更改值时需要注意什么?_Php - Fatal编程技术网

Php 在多维foreach()循环中更改值时需要注意什么?

Php 在多维foreach()循环中更改值时需要注意什么?,php,Php,我得到了一个执行以下代码的站点 $keywords = ($_SESSION[$_POST['ts']]); print_r($keywords); foreach ($keywords as $keyword) { foreach ($keyword['whitelist'] as $entry) { foreach ($_POST as $key => $value) { if ($ke

我得到了一个执行以下代码的站点

    $keywords = ($_SESSION[$_POST['ts']]);

    print_r($keywords);

    foreach ($keywords as $keyword) {
        foreach ($keyword['whitelist'] as $entry) {
            foreach ($_POST as $key => $value) {
                if ($key == $entry['encoded_url']) {
                    $entry['ignore'] = $value;
                    $decodedURL = $this->base64->url_decode($entry['encoded_url']);
                    if ($value == 'noignore') {
                        echo "found!<br />";
                        $this->blacklist_model->remove($decodedURL);
                        $html = $this->analyse->getHTML($decodedURL);
                        $entry['html'] = $html[0];
                        $entry['loading_time'] = $html[1];
                    }
                    if($value == 'alwaysignore') {
                        $this->blacklist_model->add($decodedURL);
                    }
                }
            }
        }
    }

    print_r($keywords);
$keywords=($_SESSION[$_POST['ts']]);
打印(关键字);
foreach($keywords作为$keyword){
foreach($keyword['whitelist']作为$entry){
foreach($\发布为$key=>$value){
如果($key==$entry['encoded_url']){
$entry['ignore']=$value;
$decodedURL=$this->base64->url\u decode($entry['encoded\u url']);
如果($value=='noignore'){
echo“找到了!
”; $this->blacklist\u model->remove($decodedull); $html=$this->analysis->getHTML($decodedull); $entry['html']=$html[0]; $entry['loading_time']=$html[1]; } 如果($value==“alwaysignore”){ $this->blacklist\u model->add($decodedull); } } } } } 打印(关键字);
输出如下所示:


因此,正如您所看到的,输出中有几个“find!”,因此if子句实际上执行了几次,我希望第二个数组包含新数据,如“html”,但是,正如您所看到的,没有任何变化。在多维foreach()循环中更改值时有什么需要注意的吗?

foreach
创建数组的副本并通过该副本进行循环。修改值不起作用

不过,通过使用引用,您可以绕过这个问题

foreach ($keywords as &$keyword) { 
    foreach ($keyword['whitelist'] as &$entry) { 
        foreach ($_POST as $key => &$value) { 
            ...
        }
    }
}

这样,您可以修改$value,它将影响原始数组。

您应该更改原始数组。
$entry
只是一个实例/未连接的节点。

您需要更改
$keywords
,而不是动态创建的
节点

什么是
$entry
?它包含什么?您要设置哪个值?