iversonm Posted April 15, 2012 Share Posted April 15, 2012 OK so here is what I want to do. I have a bunch of strings stored in a table. The strings are like this grandparent.parent.child. some are just parent.child What I want to do is Select all and then put them into an array. soooo x.y.z would become $Var['x']['y']['z'] and a.b would become $Var['a']['b'] It has to be dynamic because some strings are just 2 levels, and it goes all the way up to 6 levels. I have different plans for setting the variably to something but I can handle that. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/260985-string-to-array/ Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 <?php $the_string = 'HELLO WORLD'; $the_string_length = strlen($the_string); ${'tmp_array_'.($the_string_length-1)}[$the_string[$the_string_length-1]] = true; // whatever you want it to be for($i=$the_string_length-2; $i>=0; $i--){ if($i==0){ $the_array[$the_string[$i]] = ${'tmp_array_'.($i+1)}; }else{ ${'tmp_array_'.$i}[$the_string[$i]] = ${'tmp_array_'.($i+1)}; } } print_r($the_array); ?> Is this anywhere close? Quote Link to comment https://forums.phpfreaks.com/topic/260985-string-to-array/#findComment-1337574 Share on other sites More sharing options...
iversonm Posted April 15, 2012 Author Share Posted April 15, 2012 Yeah that is very close. It just needs to know sort by where the periods are vs characters. some strings are essentials.admin.all where as some are hc.chat.all so the string length varies. they need to be broken up based on the periods because that is the breaking point for the different levels. Quote Link to comment https://forums.phpfreaks.com/topic/260985-string-to-array/#findComment-1337581 Share on other sites More sharing options...
marcus Posted April 15, 2012 Share Posted April 15, 2012 $string = "lemonade.is.awesome"; list($grandparent, $parent, $child) = explode('.',$string); // grandparent = lemonade // parent = is // child = awesome Quote Link to comment https://forums.phpfreaks.com/topic/260985-string-to-array/#findComment-1337587 Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 Yeah that is very close. It just needs to know sort by where the periods are vs characters. some strings are essentials.admin.all where as some are hc.chat.all so the string length varies. they need to be broken up based on the periods because that is the breaking point for the different levels. oh, right, didn't see that to be honest. o.o' EDIT: Fixed! <?php $the_string = 'HELLO.WORLD'; $the_value = true; // whatever you want it to be $strings = explode('.', $the_string); $strings_count = count($strings); if($strings_count==1){ $the_array[$the_string] = $the_value; }else{ ${'tmp_array_'.($strings_count-1)}[$strings[$strings_count-1]] = $the_value; // whatever you want it to be for($i=$strings_count-2; $i>=0; $i--){ ${'tmp_array_'.$i}[$strings[$i]] = ${'tmp_array_'.($i+1)}; } $the_array[$strings[$i+1]] = ${'tmp_array_'.($i+2)}; } print_r($the_array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/260985-string-to-array/#findComment-1337589 Share on other sites More sharing options...
iversonm Posted April 15, 2012 Author Share Posted April 15, 2012 BOOM. Exactly what I was looking for. I just had to add to $the_string on line 3(so it would use the right column for the mysql data) it works exactly as I wanted. I will let you know if I have any thing else that comes up. Thank you for your help Quote Link to comment https://forums.phpfreaks.com/topic/260985-string-to-array/#findComment-1337597 Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 BOOM. Exactly what I was looking for. I just had to add to $the_string on line 3(so it would use the right column for the mysql data) it works exactly as I wanted. I will let you know if I have any thing else that comes up. Thank you for your help <?php function string_to_array($the_string, $the_value){ $strings = explode('.', $the_string); $strings_count = count($strings); $the_array[$strings[$strings_count-1]] = $the_value; for($i=$strings_count-2; $i>=0; $i--){ $tmp_array = $the_array; unset($the_array); $the_array[$strings[$i]] = $tmp_array; } return $the_array; } print_r(string_to_array('HELLO.WORLD', true)); ?> I rewrote it again, and this time as a function. This script is not as memory hungry either, and follows far better programming practices! Quote Link to comment https://forums.phpfreaks.com/topic/260985-string-to-array/#findComment-1337610 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.