Jump to content

[SOLVED] Array Question


dcahrakos

Recommended Posts

Hey,

 

I am currently working on a project that stores user data into a field in a mysql db like so:

username|1|1|Dcahrakos<end>

random|10|10|Just some Text<end>

 

and I need to loop through all the data in the field, and first separate it by the <end> tag, then after that is done, I need to separate it by the | tags.

 

Now, for the life of me I cant get a working script that will loop through all the data, split it, and store all the data that gets split into variables...I need to eventually parse this data for a script that will generate an image based on this data.

 

So my question is, does anyone know a good way to loop through all the data(as there will be varying amounts), then get the array's into separate variables for each? I just cant seem to  loop through and store all the data into an array for some reason, everything ive tried has failed.

any help would be greatly appreciated.

 

Thanks.

Link to comment
Share on other sites

Well right now I just have it fetching the data out of the database like so

$query = mysql_query("SELECT * from sig WHERE uid='1'") or die(mysql_error());

$result = mysql_fetch_array($query);

$textdata = $result['textdata'];

 

so after I get it from the database, then is when I have to split, and organize everything into arrays, then parse the data.

Link to comment
Share on other sites

<?php

$textdata = 'username|1|1|Dcahrakos<end>
random|10|10|Just some Text<end>';

$group = explode( '<end>', $textdata );
$arraydata = array();

foreach ( $group as $part ) {
$part = trim($part);
if ( !empty($part) )
	$arraydata[] = explode( '|', $part );
}

print_r( $arraydata );

?>

Link to comment
Share on other sites

It's also possible to make an associative array with the script, or use the username as the key of the array's first dimension. Simply store the exploded $part in a placeholder array, and define

 

$arraydata[ $placeholder[0] ] = array( 'num1' => $placeholder[1], 'num2' => $placeholder[2], 'text' => $placeholder[3] );

Link to comment
Share on other sites

The way it is now, actually works perfectly for me,

 

Just get the size of the first dimension, then do a loop through them all and parse the second dimension since the second dimensions size never changes, there will always be 4 elements in it.

 

Thanks for the help :)

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.