• <legend id='YHAnQ'><style id='YHAnQ'><dir id='YHAnQ'><q id='YHAnQ'></q></dir></style></legend>

        <bdo id='YHAnQ'></bdo><ul id='YHAnQ'></ul>
      <i id='YHAnQ'><tr id='YHAnQ'><dt id='YHAnQ'><q id='YHAnQ'><span id='YHAnQ'><b id='YHAnQ'><form id='YHAnQ'><ins id='YHAnQ'></ins><ul id='YHAnQ'></ul><sub id='YHAnQ'></sub></form><legend id='YHAnQ'></legend><bdo id='YHAnQ'><pre id='YHAnQ'><center id='YHAnQ'></center></pre></bdo></b><th id='YHAnQ'></th></span></q></dt></tr></i><div id='YHAnQ'><tfoot id='YHAnQ'></tfoot><dl id='YHAnQ'><fieldset id='YHAnQ'></fieldset></dl></div>

      <small id='YHAnQ'></small><noframes id='YHAnQ'>

    1. <tfoot id='YHAnQ'></tfoot>

        为什么我不能用 foreach 循环更新数组中的数据?

        时间:2023-09-23
        <legend id='XQAy7'><style id='XQAy7'><dir id='XQAy7'><q id='XQAy7'></q></dir></style></legend>
          <tbody id='XQAy7'></tbody>
          <bdo id='XQAy7'></bdo><ul id='XQAy7'></ul>
          1. <i id='XQAy7'><tr id='XQAy7'><dt id='XQAy7'><q id='XQAy7'><span id='XQAy7'><b id='XQAy7'><form id='XQAy7'><ins id='XQAy7'></ins><ul id='XQAy7'></ul><sub id='XQAy7'></sub></form><legend id='XQAy7'></legend><bdo id='XQAy7'><pre id='XQAy7'><center id='XQAy7'></center></pre></bdo></b><th id='XQAy7'></th></span></q></dt></tr></i><div id='XQAy7'><tfoot id='XQAy7'></tfoot><dl id='XQAy7'><fieldset id='XQAy7'></fieldset></dl></div>
              <tfoot id='XQAy7'></tfoot>

              • <small id='XQAy7'></small><noframes id='XQAy7'>

                  本文介绍了为什么我不能用 foreach 循环更新数组中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试对数组中的数据运行清理作业,特别是将纪元时间转换为 YYYY-MM-DD.

                  我最初尝试过这个功能:

                  foreach ($data as $row) {$row['eventdate'] = date('Y-m-d', $row['eventdate']);}echo '

                  ';打印_r($数据);echo '</pre>';

                  但是,当我输出数据时,foreach 循环并没有更新数据.

                  以下 for 循环确实有效:

                  for ($i=0; $i

                  为什么第一个循环失败而第二个循环成功?他们不一样吗?

                  解决方案

                  当您以目前的方式使用 foreach 循环时,foreach ($data as $row){, $row 被按值"使用,而不是按引用".

                  尝试通过将 & 添加到 $row 来更新引用:

                  foreach ($data as &$row) {$row['eventdate'] = date('Y-m-d', $row['eventdate']);

                  或者,您可以使用键/值方法:

                  foreach ($data as $index => $row) {$data[$index]['eventdate'] = date('Y-m-d', $row['eventdate']);

                  I'm trying to run a clean up job on data in an array, specifically converting epoch time to YYYY-MM-DD.

                  I tried this function originally:

                  foreach ($data as $row) {
                      $row['eventdate'] = date('Y-m-d', $row['eventdate']);
                  }
                  
                  echo '<pre>';
                  print_r($data);
                  echo '</pre>';
                  

                  However the foreach loop didn't update the data when I output it.

                  The following for loop did work:

                  for ($i=0; $i<count($data); $i++) {
                      $data[$i]['eventdate'] = date('Y-m-d', $data[$i]['eventdate']);
                  }
                  

                  Why did the first loop fail and the second work? Aren't they the same?

                  解决方案

                  When you're using a foreach loop in the way you currently are, foreach ($data as $row) {, $row is being used "by-value", not "by-reference".

                  Try updating to a reference by adding the & to the $row:

                  foreach ($data as &$row) {
                      $row['eventdate'] = date('Y-m-d', $row['eventdate']);
                  

                  Or, you can use the key/value method:

                  foreach ($data as $index => $row) {
                      $data[$index]['eventdate'] = date('Y-m-d', $row['eventdate']);
                  

                  这篇关于为什么我不能用 foreach 循环更新数组中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <small id='JqZJx'></small><noframes id='JqZJx'>

                  <legend id='JqZJx'><style id='JqZJx'><dir id='JqZJx'><q id='JqZJx'></q></dir></style></legend>

                    <bdo id='JqZJx'></bdo><ul id='JqZJx'></ul>
                  • <tfoot id='JqZJx'></tfoot>

                            <tbody id='JqZJx'></tbody>
                        1. <i id='JqZJx'><tr id='JqZJx'><dt id='JqZJx'><q id='JqZJx'><span id='JqZJx'><b id='JqZJx'><form id='JqZJx'><ins id='JqZJx'></ins><ul id='JqZJx'></ul><sub id='JqZJx'></sub></form><legend id='JqZJx'></legend><bdo id='JqZJx'><pre id='JqZJx'><center id='JqZJx'></center></pre></bdo></b><th id='JqZJx'></th></span></q></dt></tr></i><div id='JqZJx'><tfoot id='JqZJx'></tfoot><dl id='JqZJx'><fieldset id='JqZJx'></fieldset></dl></div>