How to replace a value in multidimensional array - PHP

前端 未结 1 1640
暖寄归人
暖寄归人 2021-01-15 07:22

I have a multidimensional array and I need to replace a value of a key (form_id) in it.

 $data = Array
    (
            [0] => Array
                  (
                 


        
相关标签:
1条回答
  • 2021-01-15 07:51

    I belive you can do that using array_walk_recursive.

    Here's an (untested )example :

     $data = Array
        (
                [0] => Array
                      (
                        [product_id] => 1
                        [form_id] => 18
                        [product_name] => test tet
    
                      )
    
                [1] => Array
                      (
                        [product_id] => 2
                        [form_id] => 18
                        [product_name] => test product
    
                      )
    
             )
    function array_replacing(&$item, $key)
    {
        if($key == 'form_id')
            $item = 'myform';
    }
    
    array_walk_recursive($data, 'array_replacing');
    
    0 讨论(0)
提交回复
热议问题