sKunKbad Posted December 3, 2009 Share Posted December 3, 2009 I need to be able to designate an array element dynamically, so I thought to use a variable variable, but it doesn't work: $test = array(1,2); $num = "[0]"; echo $test{"$num"}; // expecting 1 Not working. Is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/183820-variable-variables-type-question/ Share on other sites More sharing options...
premiso Posted December 3, 2009 Share Posted December 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183820-variable-variables-type-question/#findComment-970262 Share on other sites More sharing options...
sKunKbad Posted December 3, 2009 Author Share Posted December 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183820-variable-variables-type-question/#findComment-970267 Share on other sites More sharing options...
trq Posted December 3, 2009 Share Posted December 3, 2009 Still doesn't help. Your question isn't clear. Quote Link to comment https://forums.phpfreaks.com/topic/183820-variable-variables-type-question/#findComment-970268 Share on other sites More sharing options...
premiso Posted December 3, 2009 Share Posted December 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183820-variable-variables-type-question/#findComment-970272 Share on other sites More sharing options...
sKunKbad Posted December 4, 2009 Author Share Posted December 4, 2009 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); } } } } Quote Link to comment https://forums.phpfreaks.com/topic/183820-variable-variables-type-question/#findComment-971102 Share on other sites More sharing options...
Daniel0 Posted December 4, 2009 Share Posted December 4, 2009 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). Quote Link to comment https://forums.phpfreaks.com/topic/183820-variable-variables-type-question/#findComment-971103 Share on other sites More sharing options...
sKunKbad Posted December 4, 2009 Author Share Posted December 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183820-variable-variables-type-question/#findComment-971104 Share on other sites More sharing options...
Daniel0 Posted December 4, 2009 Share Posted December 4, 2009 See my edited post. I wrote something else instead when I realized what you were trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/183820-variable-variables-type-question/#findComment-971105 Share on other sites More sharing options...
sKunKbad Posted December 4, 2009 Author Share Posted December 4, 2009 Well, I guess I've got some more thinking to do. See, right now I have this working, but it's not in a recursion. It's also not limitless, which is what I was trying to make happen by using the recursion. Quote Link to comment https://forums.phpfreaks.com/topic/183820-variable-variables-type-question/#findComment-971107 Share on other sites More sharing options...
Daniel0 Posted December 4, 2009 Share Posted December 4, 2009 Indeed, so what is you trying to do, and how does your array look? Quote Link to comment https://forums.phpfreaks.com/topic/183820-variable-variables-type-question/#findComment-971112 Share on other sites More sharing options...
sKunKbad Posted December 4, 2009 Author Share Posted December 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183820-variable-variables-type-question/#findComment-971115 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.