Jump to content

$variable $variables type question


sKunKbad

Recommended Posts

Your brackets are incorrect in the echo:

 

$num = "0"; // no brackets needed here
echo $test[$num]; // [ ] are needed here

 

Not sure if that is what you are looking for, but that basically does what it seemed like you wanted done.

Link to comment
Share on other sites

Your brackets are incorrect in the echo:

 

$num = "0"; // no brackets needed here
echo $test[$num]; // [ ] are needed here

 

Not sure if that is what you are looking for, but that basically does what it seemed like you wanted done.

 

No. that's not what I'm looking for. I want to be able to add up:

 

'[0]' .= '[2]' and use the value [0][2], so this is the variable part that needs to be changing.

Link to comment
Share on other sites

I do not think your line of thinking is possible, as that is not really part of variable's variable.

 

A variable's variable is this:

 

$test = "normal";
$$test = "not normal";
echo $normal;

 

Which I am sure you have an understanding of, given the post. But that just basically creates a variable called Normal which was created dynamically in a sense. Now with arrays, the indexes cannot be interpreted from [0], as far as I know, as it just does not work.

 

Now if that is all you are doing you can easily do a str_replace on the [ and ] and remove them then use that for the index, or add the index as [0] which would also work. But as far as you are trying to use it I do not think that is possible and I think you are missing the point of variable's variable.  I could be wrong, but I have never seen anyone try to do something like that.  The real issue is that the brackets are in no part of the index of an array and php does not know to "remove" them without being told to, and thus those will throw it off.

Link to comment
Share on other sites

Well, what I'm trying to do is make the array indices concatenate within a recursive function. Right now my attempt is creating a string which php is putting into an index. See lines 8 and 11. I don't know what else to do, or how to make this work, or I wouldn't be asking!

 

<?php
function recursion($a,$b,$c, $offset='')
{
global $menu;
if(array_key_exists('sub', $c))
{
	foreach($c['sub'] as $d => $e)
	{
		$offset .= "['$d']['sub']";
		if($e['category_id'] == $a[2])
		{
			$menu[$b]['sub']{$offset}[$a[0]] = array(
				'category_id' => $a[1]
			);
			break;
		}
		else
		{
			recursion($a,$d,$e,$offset);
		}
	}
}
}

Link to comment
Share on other sites

I'm not sure you get the point of recursion. Recursion is a kind of divide and conquer which essentially involves three steps:

1) Divide the problem into subproblems.

2) Conquer the smaller subproblems.

3) Combine the subproblems.

 

This is done recursively until you reach a base case that is so small or trivial that it can be solved by itself.

 

Again it's the application of global variables that is doing someone disservice. It's really not useful for anything, and as I've said before, just makes more problems than it solves. What you want to do is to call the function recursively on the subproblems to get a partial solution.

 

Consider this function:

function myPow($a, $b)
{
if ($b == 0) return 1;

return $a * myPow($a, $b - 1);
}

The base case is that $b = 0, then the result will be 1, always. Now, figuring $a$b is the same as figuring out what $a$b-1 is (divide and conquer), and then multiplying that with $a (combine solutions).

Link to comment
Share on other sites

Can't you just do like

$menu[$b]['sub'][$d]['sub'][$a[0]]

?

 

Yes I can, but this only works for the first recursion. After that the indices need to get deeper:

<?php
// first loop
$menu[$b]['sub'][$d]['sub'][$a[0]]
// second loop
$menu[$b]['sub'][$d]['sub'][$d]['sub'][$a[0]]
// third loop
$menu[$b]['sub'][$d]['sub'][$d]['sub'][$d]['sub'][$a[0]]
// fourth loop
$menu[$b]['sub'][$d]['sub'][$d]['sub'][$d]['sub'][$d]['sub'][$a[0]]

and so on, because it is recursive (or trying to be). It's not that simple though, because with each loop, the value of $d would be set, so $d is not always the same value.

 

Link to comment
Share on other sites

It might be easier to show you what is working, and what I'm trying to convert into this recursive function:

 

<?php
public function make_category_menu()
{
$this->db->order_by('parent_id','asc');
if( $query = $this->db->get($this->config->item('categories_table')) )
{
	foreach($query->result_array() as $a)
	{
		// if top level
		if($a['parent_id'] == 0)
		{
			// create a top level category
			$menu[$a['category_name']] = array(
							'category_id' => $a['category_id']
			);
		}
		else
		{
			// level 2
			foreach($menu as $b => $c)
			{
				if($c['category_id'] == $a['parent_id'])
				{
					$menu[$b]['sub'][$a['category_name']] = array(
						'category_id' => $a['category_id']
					);
					break;
				}
				else if(array_key_exists('sub', $c))
				{
					// level 3
					foreach($c['sub'] as $d => $e)
					{
						if($e['category_id'] == $a['parent_id'])
						{
							$menu[$b]['sub'][$d]['sub'][$a['category_name']] = array(
								'category_id' => $a['category_id']
							);
							break;
						}
						else if(array_key_exists('sub', $e))
						{
							// level 4
							foreach($e['sub'] as $f => $g)
							{
								if($g['category_id'] == $a['parent_id'])
								{
									$menu[$b]['sub'][$d]['sub'][$f]['sub'][$a['category_name']] = array(
										'category_id' => $a['category_id']
									);
									break;
								}
								else if(array_key_exists('sub', $g))
								{
									// level 5
									foreach($g['sub'] as $h => $i)
									{
										if($i['category_id'] == $a['parent_id'])
										{
											$menu[$b]['sub'][$d]['sub'][$f]['sub'][$h]['sub'][$a['category_name']] = array(
												'category_id' => $a['category_id']
											);
											break;
										}
										else if(array_key_exists('sub', $i))
										{
											// level 6
											foreach($i['sub'] as $j => $k)
											{
												if($k['category_id'] == $a['parent_id'])
												{
													$menu[$b]['sub'][$d]['sub'][$f]['sub'][$h]['sub'][$j]['sub'][$a['category_name']] = array(
														'category_id' => $a['category_id']
													);
													break;
												}
												else if(array_key_exists('sub', $k))
												{
													// level 7
													foreach($k['sub'] as $l => $m)
													{
														if($m['category_id'] == $a['parent_id'])
														{
															$menu[$b]['sub'][$d]['sub'][$f]['sub'][$h]['sub'][$j]['sub'][$l]['sub'][$a['category_name']] = array(
																'category_id' => $a['category_id']
															);
															break;
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	return $this->_create_menu_lists($menu, FALSE);
}

return FALSE;
}

 

If you compare that to my attempt at the recursive function, then you might see what I am trying to achieve. I'm going to bed. 3:13AM here. Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.