2

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.

3
  • Don't undersatand :( Do you want chnge $data[0][form_id] to $data[0][Myform]? or make $data[0][form_id] = MyForm ? Commented Jul 12, 2015 at 11:08
  • I need to make $data[0][form_id] = MyForm... Commented Jul 12, 2015 at 11:14
  • Why people are down voting my question ?? Commented Jul 12, 2015 at 11:23

1 Answer 1

5

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');
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks mate , I have tested the code and it's working fine
I am glad that it helped you :) . Please mark it as answer if your problem is solved now.
yes, am trying to mark it, but it says "You cannot do that in 1 minute", let me try again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.