【问题标题】:Append value to multidimensional array PHP将值附加到多维数组 PHP
【发布时间】:2018-03-08 03:57:34
【问题描述】:

我在 PHP 中有一个多维数组,显示为:

Array
(
    [0] => Array
    (
        [background] => https://example.com/image.jpg
        [description] => Example text
        [url] => https://example.com
    )

    [1] => Array
    (
        [background] => https://example.com/image.jpg
        [description] => Example text
        [url] => https://example.com
    )
)

我想遍历这个数组并将相同的参数附加到两个url 键。我尝试通过一个带有双 foreach 循环的函数来执行此操作,并且能够成功附加参数,但我无法返回包含更新值的数组。

这是我尝试过的:

致电

$array = append_field($array, 'url', '?parameter=test');

功能

function append_field($array, $field, $parameter)
{
    foreach ($array as $inner_array) :
        foreach ($inner_array as $key => $append) :
            if ($key == $field) :
                $append .= $parameter;
            endif;
        endforeach;
    endforeach;

    return $array;
}

【问题讨论】:

    标签: php arrays multidimensional-array append


    【解决方案1】:

    您需要在两个 foreach 循环中将数组值作为引用传递才能写入它们。否则,您将迭代您的值的副本。

    参考:http://php.net/manual/en/language.references.php

    function append_field($array, $field, $parameter)
    {
        foreach ($array as &$inner_array) :
            foreach ($inner_array as $key => &$append) :
                if ($key == $field) :
                    $append .= $parameter;
                endif;
            endforeach;
        endforeach;
    
        return $array;
    }
    

    但您也可以不使用引用来执行此操作,这次是通过写入包含两个键的完整数组路径:

    function append_field($array, $field, $parameter)
    {
        foreach ($array as $i => $inner_array) :
            foreach ($inner_array as $key => $append) :
                if ($key == $field) :
                    $array[$i][$key] .= $parameter;
                endif;
            endforeach;
        endforeach;
    
        return $array;
    }
    

    【讨论】:

      【解决方案2】:

      只要改变这一行

      $append .= $parameter;
      

      到这里

      $inner_array[$key] = $append.$parameter
      

      foreach ($array as $inner_array):foreach ($array as &$inner_array) :

      【讨论】:

        【解决方案3】:

        还有一些方法可以达到同样的效果,例如使用array_map()

        [akshay@localhost tmp]$ cat test.php
        <?php
        $arr = array(
            array(
                'background'=>'https://example.com/image.jpg',
                'description'=>'Example text',
                'url'=>'https://example.com'
            ),
            array(
                'background'=>'https://example.com/image.jpg',
                'description'=>'Example text',
                'url'=>'https://example.com'
            ),
        
        );
        
        $append = array('url'=>'?parameter=test');
        print_r( 
            array_map(function($item) use ($append) {foreach($append as $k => $v){ if(isset($item[$k]))$item[$k].=$v;}return $item;}, $arr )
        );
        
        
        ?>
        

        输出:

        [akshay@localhost tmp]$ php test.php
        Array
        (
            [0] => Array
                (
                    [background] => https://example.com/image.jpg
                    [description] => Example text
                    [url] => https://example.com?parameter=test
                )
        
            [1] => Array
                (
                    [background] => https://example.com/image.jpg
                    [description] => Example text
                    [url] => https://example.com?parameter=test
                )
        
        )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-03-18
          • 2021-08-21
          • 2019-05-30
          • 1970-01-01
          • 2019-11-21
          • 2021-05-04
          • 1970-01-01
          • 2021-12-29
          相关资源
          最近更新 更多