Is there a way to convert a single dimensional array to a multidimensional array where the values of the single dimensional array become the keys to the multidimensional arrays? For example:

  $array[1] = 'I';
  $array[2] = "want";
  $array[3] = "this";
  $array[4] = "changed";

would become:

  $array['I']['want']['this']['changed'] = '';

BTW, the depth of the resulting multidimensional array is arbitrary.

TIA.

    You could use a loop and keep adding a new dimension in each loop....but I Don't understand why you'd want to do anything like this....

    Diego

      Perhaps you could give me an example of "adding a new dimension in each loop".

      What I'm trying to do is to create a tree structure. Basically, I have a bunch of topics/sub-topics that are divided by colons. Example:

        Political Parties:Republican
        Political Parties:Democrat
        Conspiracies:Assassinations:JFK
        Conspiracies:Assassinations:Martin Luther King, Jr.
        Conspiracies:UFOs
      

      What I want is to take these strings and create an array tree, so the result would be:

      'Political Parties'
        'Republican'
        'Democrat'
      'Conspiracies'
        'Assassinations'
          'JFK'
          'MLK'
        'UFOs'
      

      If you can think of a better way to make this tree from the data above, I'm all for suggestions!

        I'm sorry, I was thinking about doing this with references. But after trying 2 methods (a loop and recursion), I found out that string offsets are not compatible with references....so some other way will have to be found...

        Diego

          Write a Reply...