执行以下代码后为什么不修改数组?

时间:2015-03-12 07:43:45

标签: php arrays multidimensional-array associative-array key-value

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

答案 0 :(得分:3)

简单地说:因为那是foreach的工作方式。您获得了阵列的副本,并且您正在修改该副本。原始阵列没有改变,因为你没有对它做任何事情。

如果要更改数组,可以执行以下操作:

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

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

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

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

具有相同的结果

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

让我们阅读manual

  

为了能够直接修改循环中的数组元素,在$ value之前加上&amp ;.在这种情况下,该值将通过引用分配。

答案 3 :(得分:1)

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

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