Jump to content

string to array


iversonm

Recommended Posts

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?

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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! :)

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.