<bdo id='s8v2h'></bdo><ul id='s8v2h'></ul>
      <tfoot id='s8v2h'></tfoot><legend id='s8v2h'><style id='s8v2h'><dir id='s8v2h'><q id='s8v2h'></q></dir></style></legend>
    1. <small id='s8v2h'></small><noframes id='s8v2h'>

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

        php使用foreach将值插入数组中

        时间:2023-09-21
          <tfoot id='xY0N8'></tfoot>

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

            <bdo id='xY0N8'></bdo><ul id='xY0N8'></ul>

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

              <tbody id='xY0N8'></tbody>

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

                1. 本文介绍了php使用foreach将值插入数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个非常基本的问题,但我被卡住了.我对 php 很陌生,我有一个这样的数组:

                  I have a pretty basic question but I am stuck. I am pretty new to php and I have an array like this:

                  $array = array(
                      'one' => 1,
                      'two' => array('key1' => 'val1','key2' => 'val2'),
                      'three' => array('key1' => 'val1','key2' => 'val2'),
                      'four' => array('key1' => 'val1','key2' => 'val2')
                  );
                  

                  对于数组中的每个数组(即二"、三"和四"),我想将key3"=>val3"插入这些数组中.

                  and for each of the arrays in the array (that is, 'two, 'three', and 'four'), I want to insert 'key3' => 'val3' into those arrays.

                  我试过了:

                  foreach($array as $item) {
                      if (gettype($item) == "array") {
                          $item['key3'] = 'val3';
                      }
                  }
                  

                  但它不起作用,我不知道为什么.到处使用各种print_r,如果我在循环中将其打印出来,它似乎将'key3' => 'val3' 插入到$item 中,但原始数组似乎没有变化.我也试过一个普通的 for 循环,但也没有用.

                  But it doesn't work, and I'm not sure why. Using various print_r's all over the place, it seems to insert 'key3' => 'val3' into $item if I print it out in the loop, but the original array seems unchanged. I also tried a regular for loop but that didn't work either.

                  推荐答案

                  foreach$item 的副本一起使用,因此您无法修改 foreach 中的原始数组.解决此问题的一种方法是使用 & 运算符.

                  foreach works with a copy of $item, so you cannot modify your original array inside the foreach. One way to work around this is to use the & operator.

                  foreach($array as &$item) {
                      if (is_array($item)) {
                          $item['key3'] = 'val3';
                      }
                  }
                  

                  另一种更优雅的方法是使用 array_walk():

                  Another, more elegant way would be to use array_walk():

                  array_walk($array, function (&$v, $k) { 
                      if (is_array($v)) {
                          $v['key3'] = 'val3';
                      }
                  });
                  

                  此示例适用于 PHP 5.3,其中引入了闭包.

                  This example will work from PHP 5.3, where Closures were introduced.

                  这篇关于php使用foreach将值插入数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何将两个 foreach 循环合二为一 下一篇:foreach 相当于 jquery 中的 php?

                  相关文章

                  最新文章

                      <bdo id='og3PY'></bdo><ul id='og3PY'></ul>
                  1. <small id='og3PY'></small><noframes id='og3PY'>

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