create recursive array from single dimension array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rwt
Forum Newbie
Posts: 3
Joined: Sun May 23, 2010 4:21 pm

create recursive array from single dimension array

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: create recursive array from single dimension array

Post 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;
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: create recursive array from single dimension array

Post 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.
rwt
Forum Newbie
Posts: 3
Joined: Sun May 23, 2010 4:21 pm

Re: create recursive array from single dimension array

Post 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 !
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: create recursive array from single dimension array

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
rwt
Forum Newbie
Posts: 3
Joined: Sun May 23, 2010 4:21 pm

Re: create recursive array from single dimension array

Post by rwt »

bloody hell !!! :banghead: :banghead:
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: create recursive array from single dimension array

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply