代码之家  ›  专栏  ›  技术社区  ›  PHPFan

为什么在执行以下代码后阵列没有被修改?

  •  0
  • PHPFan  · 技术社区  · 10 年前

    我有一个名为$aRows的数组,如下所示:

    Array
    (
        [0] => Array
            (
                [is_liked] => 
                [comment_id] => 262
                [parent_id] => 0
                [type_id] => document
                [item_id] => 160
                [user_id] => 991
                [owner_user_id] => 991
                [time_stamp] => 1425364320
                [update_time] => 0
                [update_user] => 
                [rating] => 
                [ip_address] => 125.17.118.118
                [author] => 1592
                [author_email] => 
                [author_url] => 
                [view_id] => 0
                [child_total] => 0
                [total_like] => 0
                [total_dislike] => 0
                [text] => This is the long comment added to check thwe size of the comment on the device,if the size is more then add the hyperlink button to go on to the next page
                [profile_page_id] => 0
                [user_server_id] => 0
                [user_name] => profile-991
                [full_name] => disha shah
                [gender] => 0
                [user_image] => 
                [is_invisible] => 0
                [user_group_id] => 6
                [language_id] => 
                [post_convert_time] => March 3, 2015
                [profile_image] => http://52.1.47.143/theme/frontend/foxplus/style/default/image/noimage/profile_50.png
            )
    
        [1] => Array
            (
                [is_liked] => 
                [comment_id] => 263
                [parent_id] => 0
                [type_id] => document
                [item_id] => 160
                [user_id] => 991
                [owner_user_id] => 991
                [time_stamp] => 1425365110
                [update_time] => 0
                [update_user] => 
                [rating] => 
                [ip_address] => 125.17.118.118
                [author] => 1592
                [author_email] => 
                [author_url] => 
                [view_id] => 0
                [child_total] => 0
                [total_like] => 0
                [total_dislike] => 0
                [text] => This comment is of two lines need to check more about it
                [profile_page_id] => 0
                [user_server_id] => 0
                [user_name] => profile-991
                [full_name] => disha shah
                [gender] => 0
                [user_image] => 
                [is_invisible] => 0
                [user_group_id] => 6
                [language_id] => 
                [post_convert_time] => March 3, 2015
                [profile_image] => http://52.1.47.143/theme/frontend/foxplus/style/default/image/noimage/profile_50.png
            )
    
        [2] => Array
            (
                [is_liked] => 
                [comment_id] => 264
                [parent_id] => 0
                [type_id] => document
                [item_id] => 160
                [user_id] => 991
                [owner_user_id] => 991
                [time_stamp] => 1425365468
                [update_time] => 0
                [update_user] => 
                [rating] => 
                [ip_address] => 125.17.118.118
                [author] => 1592
                [author_email] => 
                [author_url] => 
                [view_id] => 0
                [child_total] => 0
                [total_like] => 0
                [total_dislike] => 0
                [text] => This comment is of two lines need to check more
                [profile_page_id] => 0
                [user_server_id] => 0
                [user_name] => profile-991
                [full_name] => disha shah
                [gender] => 0
                [user_image] => 
                [is_invisible] => 0
                [user_group_id] => 6
                [language_id] => 
                [post_convert_time] => March 3, 2015
                [profile_image] => http://52.1.47.143/theme/frontend/foxplus/style/default/image/noimage/profile_50.png
            )
    
        [3] => Array
            (
                [is_liked] => 
                [comment_id] => 372
                [parent_id] => 0
                [type_id] => document
                [item_id] => 160
                [user_id] => 991
                [owner_user_id] => 991
                [time_stamp] => 1426070317
                [update_time] => 0
                [update_user] => 
                [rating] => 
                [ip_address] => 125.17.118.118
                [author] => 1592
                [author_email] => 
                [author_url] => 
                [view_id] => 0
                [child_total] => 0
                [total_like] => 0
                [total_dislike] => 0
                [text] => Uploading Photo  for comment <div title="comment_attach_image">
    
    <a title="" title="colorbox" href="https://www.filepicker.io/api/file/CnYTVQdATAOQTkMxpAq4" ><img src="https://www.filepicker.io/api/file/CnYTVQdATAOQTkMxpAq4" height="150px" width="150px" /></a>
    
    <a href="https://www.filepicker.io/api/file/CnYTVQdATAOQTkMxpAq4" class="comment_attach_image_link_dwl">Download</a>
    
    </div>
                [profile_page_id] => 0
                [user_server_id] => 0
                [user_name] => profile-991
                [full_name] => disha shah
                [gender] => 0
                [user_image] => 
                [is_invisible] => 0
                [user_group_id] => 6
                [language_id] => 
                [post_convert_time] => 21 hours ago
                [profile_image] => http://52.1.47.143/theme/frontend/foxplus/style/default/image/noimage/profile_50.png
            )
    
        [4] => Array
            (
                [is_liked] => 
                [comment_id] => 375
                [parent_id] => 0
                [type_id] => document
                [item_id] => 160
                [user_id] => 991
                [owner_user_id] => 991
                [time_stamp] => 1426070656
                [update_time] => 0
                [update_user] => 
                [rating] => 
                [ip_address] => 106.79.139.101
                [author] => 1592
                [author_email] => 
                [author_url] => 
                [view_id] => 0
                [child_total] => 0
                [total_like] => 0
                [total_dislike] => 0
                [text] => test
                [profile_page_id] => 0
                [user_server_id] => 0
                [user_name] => profile-991
                [full_name] => disha shah
                [gender] => 0
                [user_image] => 
                [is_invisible] => 0
                [user_group_id] => 6
                [language_id] => 
                [post_convert_time] => 21 hours ago
                [profile_image] => http://52.1.47.143/theme/frontend/foxplus/style/default/image/noimage/profile_50.png
            )
    
        [5] => Array
            (
                [is_liked] => 
                [comment_id] => 376
                [parent_id] => 0
                [type_id] => document
                [item_id] => 160
                [user_id] => 991
                [owner_user_id] => 991
                [time_stamp] => 1426072374
                [update_time] => 0
                [update_user] => 
                [rating] => 
                [ip_address] => 125.17.118.118
                [author] => 1592
                [author_email] => 
                [author_url] => 
                [view_id] => 0
                [child_total] => 0
                [total_like] => 0
                [total_dislike] => 0
                [text] => Amit&#039;s pic<div class="comment_attach_image">
                <a class="group1 cboxElement" href="http://52.1.47.143/file/attachment/2015/03/e55f0f3080eb9828270a7963648a5826.jpeg" ><img src="http://52.1.47.143/file/attachment/2015/03/e55f0f3080eb9828270a7963648a5826.jpeg" height="150px" width="150px" /></a>
    
                <a class="comment_attach_image_link_dwl"  href="http://52.1.47.143/feed/download/year_2015/month_03/file_e55f0f3080eb9828270a7963648a5826.jpeg" >Download</a>
                </div>
                [profile_page_id] => 0
                [user_server_id] => 0
                [user_name] => profile-991
                [full_name] => disha shah
                [gender] => 0
                [user_image] => 
                [is_invisible] => 0
                [user_group_id] => 6
                [language_id] => 
                [post_convert_time] => 20 hours ago
                [profile_image] => http://52.1.47.143/theme/frontend/foxplus/style/default/image/noimage/profile_50.png
            )
    
        [6] => Array
            (
                [is_liked] => 
                [comment_id] => 377
                [parent_id] => 0
                [type_id] => document
                [item_id] => 160
                [user_id] => 991
                [owner_user_id] => 991
                [time_stamp] => 1426072419
                [update_time] => 0
                [update_user] => 
                [rating] => 
                [ip_address] => 125.17.118.118
                [author] => 1592
                [author_email] => 
                [author_url] => 
                [view_id] => 0
                [child_total] => 0
                [total_like] => 0
                [total_dislike] => 0
                [text] => PDF file added<div class="comment_attach_file">
                <a class="comment_attach_file_link" href="http://52.1.47.143/feed/download/year_2015/month_03/file_1b87d4420c693f2bbdf738cbf2457d89.pdf" >1b87d4420c693f2bbdf738cbf2457d89.pdf</a>
    
                <a class="comment_attach_file_link_dwl"  href="http://52.1.47.143/feed/download/year_2015/month_03/file_1b87d4420c693f2bbdf738cbf2457d89.pdf" >Download</a>
                </div>
                [profile_page_id] => 0
                [user_server_id] => 0
                [user_name] => profile-991
                [full_name] => disha shah
                [gender] => 0
                [user_image] => 
                [is_invisible] => 0
                [user_group_id] => 6
                [language_id] => 
                [post_convert_time] => 20 hours ago
                [profile_image] => http://52.1.47.143/theme/frontend/foxplus/style/default/image/noimage/profile_50.png
            )
    
        [7] => Array
            (
                [is_liked] => 
                [comment_id] => 378
                [parent_id] => 0
                [type_id] => document
                [item_id] => 160
                [user_id] => 991
                [owner_user_id] => 991
                [time_stamp] => 1426075869
                [update_time] => 0
                [update_user] => 
                [rating] => 
                [ip_address] => 125.17.118.118
                [author] => 1592
                [author_email] => 
                [author_url] => 
                [view_id] => 0
                [child_total] => 0
                [total_like] => 0
                [total_dislike] => 0
                [text] => Just did it...
                [profile_page_id] => 0
                [user_server_id] => 0
                [user_name] => profile-991
                [full_name] => disha shah
                [gender] => 0
                [user_image] => 
                [is_invisible] => 0
                [user_group_id] => 6
                [language_id] => 
                [post_convert_time] => 20 hours ago
                [profile_image] => http://52.1.47.143/theme/frontend/foxplus/style/default/image/noimage/profile_50.png
            )
    
        [8] => Array
            (
                [is_liked] => 
                [comment_id] => 379
                [parent_id] => 0
                [type_id] => document
                [item_id] => 160
                [user_id] => 991
                [owner_user_id] => 991
                [time_stamp] => 1426094874
                [update_time] => 0
                [update_user] => 
                [rating] => 
                [ip_address] => 117.200.213.90
                [author] => 1592
                [author_email] => 
                [author_url] => 
                [view_id] => 0
                [child_total] => 0
                [total_like] => 0
                [total_dislike] => 0
                [text] => Profile photo uploaded<div class="comment_attach_image">
                <a class="group1 cboxElement" href="http://52.1.47.143/file/attachment/2015/03/a4ea5532b83a56bbbae2fffc80de4fee.png" ><img src="http://52.1.47.143/file/attachment/2015/03/a4ea5532b83a56bbbae2fffc80de4fee.png" height="150px" width="150px" /></a>
    
                <a class="comment_attach_image_link_dwl"  href="http://52.1.47.143/feed/download/year_2015/month_03/file_a4ea5532b83a56bbbae2fffc80de4fee.png" >Download</a>
                </div>
                [profile_page_id] => 0
                [user_server_id] => 0
                [user_name] => profile-991
                [full_name] => disha shah
                [gender] => 0
                [user_image] => 
                [is_invisible] => 0
                [user_group_id] => 6
                [language_id] => 
                [post_convert_time] => 14 hours ago
                [profile_image] => http://52.1.47.143/theme/frontend/foxplus/style/default/image/noimage/profile_50.png
            )
    
        [9] => Array
            (
                [is_liked] => 
                [comment_id] => 383
                [parent_id] => 0
                [type_id] => document
                [item_id] => 160
                [user_id] => 991
                [owner_user_id] => 991
                [time_stamp] => 1426139915
                [update_time] => 0
                [update_user] => 
                [rating] => 
                [ip_address] => 125.17.118.118
                [author] => 1592
                [author_email] => 
                [author_url] => 
                [view_id] => 0
                [child_total] => 0
                [total_like] => 0
                [total_dislike] => 0
                [text] => Akki <div title="comment_attach_image">
    
    <a title="" title="colorbox" href="https://www.filepicker.io/api/file/NJqijbKTIOA0ZJBNknsm" ><img src="https://www.filepicker.io/api/file/NJqijbKTIOA0ZJBNknsm" height="150px" width="150px" /></a>
    
    <a href="https://www.filepicker.io/api/file/NJqijbKTIOA0ZJBNknsm" class="comment_attach_image_link_dwl">Download</a>
    
    </div>
                [profile_page_id] => 0
                [user_server_id] => 0
                [user_name] => profile-991
                [full_name] => disha shah
                [gender] => 0
                [user_image] => 
                [is_invisible] => 0
                [user_group_id] => 6
                [language_id] => 
                [post_convert_time] => 2 hours ago
                [profile_image] => http://52.1.47.143/theme/frontend/foxplus/style/default/image/noimage/profile_50.png
            )
    
    )
    

    现在我已经编写了以下代码来处理数组$aRows,但我再次得到了相同的数组。为什么?

    foreach($aRows as $comment) {
        $text = strstr($comment['text'], '<div');
        if (strlen($text) <= 0) {
          $comment['type_id'] =  'text';
          $comment['url'] =  '';
          $comment['text'] =  $comment['text'];
        } else if($xml = @simplexml_load_string($text)) {
          $comment['type_id'] =  substr(strrchr($xml['class'], '_'), 1);
          $comment['url'] = $xml->a['href']->asXML();
          $comment['text'] =  strtok($comment['text'], '<');           
        } else {
          continue;
        };
    }
    print_r($aRows); die;//It prints the same array again
    

    谢谢

    4 回复  |  直到 10 年前
        1
  •  3
  •   Nanne    10 年前

    简单地说:因为这就是 foreach 作品你得到了 复制 然后修改该副本。原始数组没有改变,因为你没有对它做任何操作。

    如果要更改阵列,可以执行以下操作:

    foreach($aRows as $key=>$comment) {
        $aRows[$key]['url'] = '';
    }
    

    现在您正在更改原始阵列。

    正如@darbee在评论中所说,您可以使用 & 在foreach中进行引用,如下所示:

    foreach($aRows as &$comment) {
        $comment['url'] = 'http://stackoverflow.com'
    }
    

    结果相同

        2
  •  2
  •   Carlos    10 年前

    那是因为 $comment 不是 reference 到原始数组元素,但只是一个副本。您应该这样做:

    foreach ($aRows as $i => $comment) {
        ...
        $aRows[$i]['type_id'] = 'text';
        ...
    }
    
        3
  •  1
  •   tutankhamun    10 年前

    让我们阅读 manual

    为了能够直接修改循环中的数组元素,在$value前面加上&。在这种情况下,将通过引用指定值。

        4
  •  1
  •   Volvox    10 年前

    不同的方法是引用数组元素而不是复制:

    foreach ($aRows as &$comment){
      $comment['url'] = 'http://newurl.com';
    }
    
    推荐文章