Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319431
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319446
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319457
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319470
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319541
Share on other sites

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

?>

Link to comment
https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-319631
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/64100-solved-storing-arrays/#findComment-320481
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.