cry of war Posted August 9, 2007 Share Posted August 9, 2007 is there any way that i can store a array as a variable because i dont want to creat a database for thousands of variables. heres what i thought of so far then i just kind of got stuck <?PHP $arrayval = "1,3,2,7,4,6,8,15,12,78,96,1203,1029,39509,12999"; $array = array($arrayval); echo $array[1],$array[3],$array[2],$array[5],$array[6],$array[4],$array[7],$array[9],$array[5],$array[10],$array[13]; echo "<hr>"; echo $arrayval; echo "<hr>"; $sum = $array[1]+$array[3]+$array[2]+$array[5]+$array[6]+$array[4]+$array[7]+$array[9]+$array[5]+$array[10]+$array[13]; echo $sum; ?> Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/ Share on other sites More sharing options...
trq Posted August 9, 2007 Share Posted August 9, 2007 Sorry, your question is pretty unclear. Do you want to store an array for later use, or echo all values within an array? If the later, use a loop. eg; <?php $arr = array(1,2,3,4,5,6,7,8,9); foreach($arr as $val) { echo $val . "<br />"; } ?> And you can use serialize to create a string representation of an array, usefull for storage. Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319431 Share on other sites More sharing options...
cry of war Posted August 9, 2007 Author Share Posted August 9, 2007 i would like to set a str say 1,0,0,1,0,1,0,1,0,1,1,1,1,0 and so on where each value means that the person has that trait or not. http://www.cryofwar.phpnet.us/index.php near the bottom of this page is where this will be usefull. I have the php page set up so if a certain var equals 1 or 0 it will determine if that area displays or not other wise im going to have a huge list of values in my database and i dont want that. so i would $arr=array(1,0,0,1,0,1,0,1,0,1,1,1,1,0) and where $arr[1]=1 the data in that section would show but if $arr[1]=0 the data wouldnt show. this is the code im going to be using this in if it will help any and i know the codes not set up for this i just need to figure out what i need to change it too before i will change it <table class=table_lines style="PADDING-LEFT: 0px; PADDING-RIGHT: 25px" cellspacing="0" cellpadding="5" width="100%" border="0"> <tbody> <tr><td align="center" colspan="4"><font color=""><B>Armor Proficiency</font></td></tr> <tr><td align="center"><font color=""><B>FEAT</B></font></td><td align="center"><font color="">DESCRIPTION</font></td><td align="center"><font color="">PREREQUISITE<br></font></td><td align="center"><font color="">On/Off</font></td></tr> <?php $armorproficiencylight=1; $armorproficiencymedium=0; $armorproficiencyheavy=1; $monkeygrip=0; $shieldproficiency=1; $towershield=0; ?> /***so the str would be stored as $armorproficiencylight,$armorproficiencymedium,$armorproficiencyheavy,$monkeygrip,$shieldproficiency,$towershield or 1,0,1,0,1,0 for later use ****/ <?php if ($armorproficiencylight == 1){echo "<tr><td align=\"center\"><font color=\"\"><B>Armor Proficiency, Light</B></font></td><td align=\"center\"><font color=\"\">Can equip light armor.</font></td><td align=\"center\"><font color=\"\">Armor Proficiency, Light</font></td><td align=\"center\"><font color=\"\"><input type=\"radio\" name=\" \" value=\"1\">Active<br><input type=\"radio\" name=\" \" value=\"0\">Inactive<br></font></td></tr>";} else { echo "";} if ($armorproficiencymedium == 1){echo "<tr><td align=\"center\"><font color=\"\"><B>Armor Proficiency, Medium</B></font></td><td align=\"center\"><font color=\"\">Can equip medium armor.</font></td><td align=\"center\"><font color=\"\">Armor Proficiency, Medium</font></td><td align=\"center\"><font color=\"\"><input type=\"radio\" name=\" \" value=\"1\">Active<br><input type=\"radio\" name=\" \" value=\"0\">Inactive<br></font></td></tr>";} else { echo "";} if ($armorproficiencyheavy == 1){echo "<tr><td align=\"center\"><font color=\"\"><B>Armor Proficiency, Heavy</B></font></td><td align=\"center\"><font color=\"\">Can equip heavy armor.</font></td><td align=\"center\"><font color=\"\">Armor Proficiency, Heavy</font></td><td align=\"center\"><font color=\"\"><input type=\"radio\" name=\" \" value=\"1\">Active<br><input type=\"radio\" name=\" \" value=\"0\">Inactive<br></font></td></tr>";} else { echo "";} if ($monkeygrip == 1){echo "<tr><td align=\"center\"><font color=\"\"><B>Monkey Grip</B></font></td><td align=\"center\"><font color=\"\">Can wield two-handed weapon in one hand.</font></td><td align=\"center\"><font color=\"\">Base attack bonus +1</font></td><td align=\"center\"><font color=\"\"><input type=\"radio\" name=\" \" value=\"1\">Active<br><input type=\"radio\" name=\" \" value=\"0\">Inactive<br></font></td></tr>";} else { echo "";} if ($shieldproficiency == 1){echo "<tr><td align=\"center\"><font color=\"\"><B>Shield Proficiency</B></font></td><td align=\"center\"><font color=\"\">Can equip a shield.</font></td><td align=\"center\"><font color=\"\">Shield</font></td><td align=\"center\"><font color=\"\"><input type=\"radio\" name=\" \" value=\"1\">Active<br><input type=\"radio\" name=\" \" value=\"0\">Inactive<br></font></td></tr>";} else { echo "";} if ($towershield == 1){echo "<tr><td align=\"center\"><font color=\"\"><B>Tower Shield</B></font></td><td align=\"center\"><font color=\"\">Can equip a Tower Shield.</font></td><td align=\"center\"><font color=\"\">Shield Proficiency</font></td><td align=\"center\"><font color=\"\"><input type=\"radio\" name=\" \" value=\"1\">Active<br><input type=\"radio\" name=\" \" value=\"0\">Inactive<br></font></td></tr>";} else { echo "";} ?> the file is like 20 times bigger then this thats why i need to know if i can store information like this Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319446 Share on other sites More sharing options...
trq Posted August 9, 2007 Share Posted August 9, 2007 Sorry... your question is still very unclear. Do you want to turn a string.... $str = "1,0,0,1,0,1,0,1,0,1,1,1,1,0"; into an array? <?php $str = "1,0,0,1,0,1,0,1,0,1,1,1,1,0"; $arr = explode(',',$str); ?> Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319450 Share on other sites More sharing options...
cry of war Posted August 9, 2007 Author Share Posted August 9, 2007 hmm how to explain i need to store a large about of numbers (mostly 1s and 0s) in this form #,#,#,#,#,#,#,#,# so i can later break it back down into different vars that gives me a great amount of information about the users stats and the above would work but i need to keep them as numbers and not strings. Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319457 Share on other sites More sharing options...
trq Posted August 9, 2007 Share Posted August 9, 2007 Where are you going to store them? Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319462 Share on other sites More sharing options...
cry of war Posted August 9, 2007 Author Share Posted August 9, 2007 ok im going to store #,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# in a table (along with userid pass and soon) so when the user comes back later the php page will break it back down into yes the user has this option or no the user doesnt have the option Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319465 Share on other sites More sharing options...
lemmin Posted August 9, 2007 Share Posted August 9, 2007 I don't know for sure, but can't you serialize the array and store it in the table? Then you wouldn't need to parse the string (not that that would be difficult.) The only thing I am unsure of is how large the serialization of an array would be. Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319466 Share on other sites More sharing options...
cry of war Posted August 9, 2007 Author Share Posted August 9, 2007 well i would have like 120+ values that i would need to plug into it Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319469 Share on other sites More sharing options...
trq Posted August 9, 2007 Share Posted August 9, 2007 ok im going to store #,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# in a table (along with userid pass and soon) so when the user comes back later the php page will break it back down into yes the user has this option or no the user doesnt have the option So whats the problem? Pull that string back out and turn it into an array. Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319470 Share on other sites More sharing options...
cry of war Posted August 9, 2007 Author Share Posted August 9, 2007 i tried that all i get back is errors saying Notice: Undefined offset: 1 in C:\apache2triad\htdocs\tryarray.php on line 4 Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319474 Share on other sites More sharing options...
trq Posted August 9, 2007 Share Posted August 9, 2007 We need to see your code then. Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319505 Share on other sites More sharing options...
cry of war Posted August 9, 2007 Author Share Posted August 9, 2007 <?PHP $arrayval = "1,3,2,7,4,6,8,15,12,78,96,1203,1029,39509,12999"; $array = array($arrayval); echo $array[1],$array[3],$array[2],$array[5],$array[6],$array[4],$array[7],$array[9],$array[5],$array[10],$array[13]; echo "<hr>"; echo $arrayval; echo "<hr>"; $sum = $array[1]+$array[3]+$array[2]+$array[5]+$array[6]+$array[4]+$array[7]+$array[9]+$array[5]+$array[10]+$array[13]; echo $sum; ?> Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319541 Share on other sites More sharing options...
Barand Posted August 9, 2007 Share Posted August 9, 2007 why does your code still contain $arrayval = "1,3,2,7,4,6,8,15,12,78,96,1203,1029,39509,12999"; $array = array($arrayval); when Thorpe gave you ?php $str = "1,0,0,1,0,1,0,1,0,1,1,1,1,0"; $arr = explode(',',$str); ?> Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319631 Share on other sites More sharing options...
cry of war Posted August 10, 2007 Author Share Posted August 10, 2007 would explode keep the numbers as numbers or turn them into strings? Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-320175 Share on other sites More sharing options...
cry of war Posted August 10, 2007 Author Share Posted August 10, 2007 ok never mind thropes way does work i thought explode turned the values into a string and not a variable so the numbers wouldnt add when i needed them to Thank you Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-320185 Share on other sites More sharing options...
Barand Posted August 10, 2007 Share Posted August 10, 2007 They will be string, but as PHP is a typeless language you can treat them as either. Or can force them to be int <?php $str = "1,0,0,1,0,1,0,1,0,1,1,1,1,0"; $arr = explode(',',$str); echo '<pre>'; var_dump($arr) ; echo '</pre>'; foreach($arr as $k=>$v) $arr[$k] = intval($v); echo '<pre> After convert to int'; var_dump($arr) ; echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-320481 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.