Page 1 of 1

create recursive array from single dimension array

Posted: Sun May 23, 2010 4:34 pm
by rwt
Okay, this is going to be hard to explain… it a logical problem.

I have an array (which I obtained from a preg_replace). This array has keys going from 0 to an unknown number.
What I need it to loop for the lenght of the array and create a new multidimensional array of an imbrication by ascending order. :banghead:

An example will help clarifying:
base array :

Code: Select all

[0]=>'hello'
[1]=>'how'
[2]=>'are'
[3]=>'you'
final result desired :

Code: Select all

['hello']['how']['are']=>'you'
I just cannot figure out how to do this, but I pretty sure it's simple. Please help.

Re: create recursive array from single dimension array

Posted: Sun May 23, 2010 4:46 pm
by requinix
Congratulations: I'll post code rather than explain it. No, it's not complicated - I just don't feel like writing some paragraph about it.

Code: Select all

$old_array = array(
	0 => 'hello',
	1 => 'how',
	2 => 'are',
	3 => 'you'
);
// last item is treated specially
$last = array_pop($old_array);

$new_array = array();
$current =& $new_array; // the furthest into $new_array
foreach ($old_array as $item) {
	$current[$item] = array();
	$current =& $current[$item];
}
$current = $last;

Re: create recursive array from single dimension array

Posted: Sun May 23, 2010 9:24 pm
by Jonah Bron
But a question that must be asked is, why? If you are so disposed, please explain the the purpose of that structure. We may be able to dissuade you from a poor method of achieving something.

Re: create recursive array from single dimension array

Posted: Mon May 24, 2010 12:43 pm
by rwt
tasairis > thank you very much, it works just fine. I didn't quite understand the =& operator. In the php documentation it talks about making a clone but from what I understand its to move the pointer ??

Jonah Bron > yes, thanks! Here's how it goes:
I created a configuration file which is text only and more user friendly than putting actual php code in.
It's basically very simple, it allows to create "vars" in the following format:

[text]#var_name = content of that var[/text]

or with children :

[text]#var_name[child][child2] = content[/text]

I then create an array from that file using file() and then return an array with all the valid vars.
I know it take a lot more work for php to process that, but it is only used once or twice. Actually it's used for stuff like database connection information and such.

Just for fun, here's the code. Let me know if you see any faster and easier way to do it :

Code: Select all

// CREATE ARRAY FROM FILE
define('FILETOARRAY_NBR_ALLOWED',6);

function file_to_array($file)
{
	
	$reg_is_var = '/^\t*\#(?P<name>[a-z_\.]+[0-9a-z_\.]*)'; // IS A VAR ?
	for($i=1;$i<=FILETOARRAY_NBR_ALLOWED;$i++){
		$reg_is_var .= '(\[(?P<key'.$i.'>[a-z_\.0-9]*)\])?'; // Keys
	}
	$reg_is_var .= '\s?\=\s?(?P<value>.+)$/'; // Value

	$array = array();
	foreach ($file as $line_num => $line) {
		if(preg_match($reg_is_var,$line)){
			preg_match($reg_is_var,$line,$rslt);
			// check for key 1,2,3 and 4
			$is_array = false; // assuming array keys are all empty
			$tmp = array($rslt['name']);
			for($i=1;$i<=FILETOARRAY_NBR_ALLOWED;$i++){
				if(isset($rslt['key'.$i]) && !empty($rslt['key'.$i])){
					$is_array = true;
					array_push($tmp,$rslt['key'.$i]);
				}else{
					break;
				}
			}
			if($is_array){
				array_push($tmp,$rslt['value']);
				$last = array_pop($tmp);
				
				$new_array = array();
				$current =& $new_array; // the furthest into $new_array
				foreach ($tmp as $item) {
						$current[$item] = array();
						$current =& $current[$item];
				}
				$current = $last;
				
				$array = array_merge_recursive($array,$new_array);
			}else{
				$array[$rslt['name']] = $rslt['value']; // Set default (kept if not an array)
			}
			
		}
	}
	return $array;
}
cheers !

Re: create recursive array from single dimension array

Posted: Mon May 24, 2010 1:09 pm
by AbraCadaver
rwt wrote:Just for fun, here's the code. Let me know if you see any faster and easier way to do it :
Why reinvent the wheel? http://us.php.net/manual/en/function.parse-ini-file.php

Re: create recursive array from single dimension array

Posted: Mon May 24, 2010 1:28 pm
by rwt
bloody hell !!! :banghead: :banghead:

Re: create recursive array from single dimension array

Posted: Mon May 24, 2010 1:32 pm
by AbraCadaver
rwt wrote:bloody hell !!! :banghead: :banghead:
It won't give you the format that you've been using, but it is fairly flexible and is an ini format that is fairly common to other technologies.