Почему массив не изменяется после выполнения следующего кода?

У меня есть массив под названием $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

Спасибо.


person PHPFan    schedule 12.03.2015    source источник


Ответы (4)


Это потому, что $comment является не ссылкой на исходный элемент массива, а просто копией. Вы должны сделать это:

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

Проще говоря: потому что так работает foreach. Вы получаете копию своего массива и изменяете эту копию. Исходный массив не изменяется, потому что вы ничего с ним не делаете.

Если вы хотите изменить массив, вы можете сделать что-то вроде этого:

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

Теперь вы меняете исходный массив.

Как говорит @darbee в комментарии, вы можете использовать & для создания ссылки в foreach, например:

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

с тем же результатом

person Nanne    schedule 12.03.2015
comment
Итак, что мне делать, чтобы изменить исходный массив? - person PHPFan; 12.03.2015
comment
Или вы можете сделать foreach($aRows as &$comment) {, чтобы избежать копирования массива - person DarkBee; 12.03.2015
comment
Совершенно верно, я обычно не использую многие из этих ссылок в PHP, но это допустимый вариант. давайте добавим это к ответу - person Nanne; 12.03.2015

Давайте прочитаем руководство.

Чтобы иметь возможность напрямую изменять элементы массива в цикле, перед $value ставится знак &. В этом случае значение будет присвоено по ссылке.

person tutankhamun    schedule 12.03.2015

Другой способ сделать это - ссылаться на элемент массива вместо копирования:

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