0
votes

I've an array titled $aRows as follows :

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
        )

)

Now I've written following code to manipulate the array $aRows but I'm getting the same array again. Why so?

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

Thanks.

4

4 Answers

3
votes

Simply put: because that's how foreach works. You get a copy of your array, and you're modifying that copy. The origional array isn't changed because you're not doing anything to it.

If you want to change the array, you could do something like this:

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

Now you're changing the origional array.

As @darbee says in the comment, you can use an & to make a reference in an foreach, like so:

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

with the same result

2
votes

That's because $comment is not a reference to the original array element but just a copy. You should do this:

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

Let's read the 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.

1
votes

Different way of doing this is to take reference to array element instead of copy:

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