Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Create multidimensional array from array

☮TAKU????
Loyal Member
Joined
Nov 16, 2009
Messages
866
Reaction score
580
Hello

I am stuck on a function I need to create in order to manage my cache from a custom admin panel. Can anyone of you think of a possible way to create a function that will create a multidimensional array from an array with values separated by dots. Because I certainly cannot after trying 3 hours.

It's hard to explain what i mean but perhaps this will make you understand more:
PHP:
Array
(
    [0] => ragephp.config.website
    [1] => ragephp.config.server
    [2] => ragephp.stats
    [3] => rageweb.locale.page-index
    [4] => rageweb.locale.page-register
    [5] => ragezone
)

Would look something like this when it has been "translated":
PHP:
Array
(
    [ragephp] => Array
        (
            [config] => Array
                (
                    [website] => 
                    [server] => 
                )

            [stats] => 
        )

    [rageweb] => Array
        (
            [locale] => Array
                (
                    [page-index] => 
                    [page-register] => 
                )

        )

    [ragezone] => 
)
 
Joined
Jun 23, 2010
Messages
2,323
Reaction score
2,195
PHP:
<?php

	function test($input)
	{
  $array = array();
  
  foreach ($input as $value)
  {
   $depth =& $array;
   $keys = explode('.', $value);
   foreach ($keys as $key)
   {
    $depth =& $depth[$key];
   }
  }
  
  return $array;
	}
	
	var_dump(test(array(
  'ragephp.config.website',
  'ragephp.config.server',
  'ragephp.stats',
  'rageweb.locale.page-index',
  'rageweb.locale.page-register',
  'ragezone'
	)));
 
☮TAKU????
Loyal Member
Joined
Nov 16, 2009
Messages
866
Reaction score
580
PHP:
<?php

	function test($input)
	{
  $array = array();
  
  foreach ($input as $value)
  {
   $depth =& $array;
   $keys = explode('.', $value);
   foreach ($keys as $key)
   {
    $depth =& $depth[$key];
   }
  }
  
  return $array;
	}
	
	var_dump(test(array(
  'ragephp.config.website',
  'ragephp.config.server',
  'ragephp.stats',
  'rageweb.locale.page-index',
  'rageweb.locale.page-register',
  'ragezone'
	)));

Any way to change it to array values instead of keys?
 
Joined
Jun 23, 2010
Messages
2,323
Reaction score
2,195
Any way to change it to array values instead of keys?

PHP:
<?php

    function test($input)
    {
  $array = array();
  
  foreach ($input as $value)
  {
   $depth =& $array;
   $keys = explode('.', $value);
   $value = array_pop($keys);
   foreach ($keys as $key)
   {
    $depth =& $depth[$key];
   }
   
   $depth[] = $value;
  }
  
  return $array;
    }

Would be the clousest result.
 
☮TAKU????
Loyal Member
Joined
Nov 16, 2009
Messages
866
Reaction score
580
PHP:
<?php

	function test($input)
	{
  $array = array();
  
  foreach ($input as $value)
  {
   $depth =& $array;
   $keys = explode('.', $value);
   foreach ($keys as $key)
   {
    $depth =& $depth[$key];
   }
  }
  
  return $array;
	}
	
	var_dump(test(array(
  'ragephp.config.website',
  'ragephp.config.server',
  'ragephp.stats',
  'rageweb.locale.page-index',
  'rageweb.locale.page-register',
  'ragezone'
	)));

Would you mind to explain what is going on here? Joopie
 
Joined
Jun 23, 2010
Messages
2,323
Reaction score
2,195
Would you mind to explain what is going on here? @Joopie

$array holds the new created array
$input are the string values to be converted

for each $value in $input you create a fresh reference $depth to $array.
So $depth is a pointer to the variable $array

The keys you want are seperated with a ".", hence the explode to get an array of those keys.

Looping over all those keys, and just make a new reference and override the old one.

For example: "ragephp.config.website" you get the keys (ragephp, config, website).
And the loop will be as followed:

$depth points to $array
$depth[ragephp] (which points to $array[ragephp]) overrides $depth
$depth[config] (which points to $array[ragephp][config]) overrides $depth
$depth[website] (which points to $array[ragephp][config][website]) overrides $depth
And so on.
 
Back
Top