How to replace a value in multidimensional array - PHP

倖福魔咒の 提交于 2019-12-23 02:39:01

问题


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

 $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

                  )

         )

after replacing the "form_id" with value "My Form" then i need to return the whole multidimensional array. Please give me a solution, thanks in advance.


回答1:


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');


来源:https://stackoverflow.com/questions/31367055/how-to-replace-a-value-in-multidimensional-array-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!