本文介绍了如何替换多维数组中的值-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维数组,需要替换其中的键(form_id)值.

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

                  )

         )

将" form_id "替换为值"我的表单"之后,我需要返回整个多维数组.请给我一个解决方案,谢谢.

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.

推荐答案

我相信您可以使用 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');

这篇关于如何替换多维数组中的值-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 04:52