1

I have a string format for pushing an item into a recursive array.

Ex. CEN_SCH_SET_FUL_INT_FAS or CEN_SCH_BUS_FUL_ACC_DIP or CEN_SCH_BUS

What I am trying to do is, insert a specific value ex CEN_SCH_BUS will be inserted into a multidimensional array with the keys ex. $global_array['CEN']['SCH']['BUS'] = "CEN_SCH_BUS"

and same for the other cases.

I have tried to with,

array_walk_recursive($value_toinsert , function($item, $key) {
    $global_array[$key] = $value_toinsert;
});

but can't see it to work.

One example insertion of CEN_SCH_SET_FUL_TEC, so CEN_SCH_SET_FUL_TEC is inserted at CEN > SCH > SET > FUL

Array
(
    [CEN] => Array
        (
            [SCH] => Array
                (
                    [SET] => Array
                        (
                            [FUL] => CEN_SCH_SET_FUL_TEC
                        )

                )

        )

)

Note: I want to keep adding the other values in the array. It can be within the existing key indexes or different (kind of a tree). Not just one value.

Bhavesh G
  • 3,000
  • 4
  • 39
  • 66
  • What is the result of above function and how it differs to intended outcome? – Rahi Jan 26 '17 at 18:24
  • Check my edited question. – Bhavesh G Jan 26 '17 at 18:28
  • To use the dupe: `$path = explode('_', $value); array_pop($path);` – AbraCadaver Jan 26 '17 at 18:44
  • You just need to convert your string value into an array that matches your requirement (use references to continuously create keys within keys), then merge the result recursively with your target array. – georaldc Jan 26 '17 at 18:49
  • yes. I need to explode and then use recursive but I can't get it. – Bhavesh G Jan 26 '17 at 18:52
  • I *might* have an answer (if I understood your problem right) but your question has been marked as a duplicate already. Maybe you can find the solution there – georaldc Jan 26 '17 at 18:54
  • I don't think it is duplicate. What I want is, keep adding more values in the array. The key might be same and different. – Bhavesh G Jan 27 '17 at 03:14

0 Answers0