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

Why the array is not getting modified after executing following code?

我有一个名为 $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

谢谢。

简单地说:因为这就是 foreach 的工作原理。您获得数组的 copy,并且正在修改该副本。原始数组没有改变,因为你没有对它做任何事情。

如果你想改变数组,你可以这样做:

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

现在您正在更改原始数组。

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

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

结果相同

那是因为 $comment 不是原始数组元素的 reference 而只是一个副本。你应该这样做:

foreach ($aRows as $i => $comment) {
    ...
    $aRows[$i]['type_id'] = 'text';
    ...
}

让我们阅读manual

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

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

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