Page 1 of 1

Defining an array with an array of keys and a variable name.

Posted: Wed Mar 09, 2005 8:31 pm
by Ambush Commander
As of now, I have this code.

Code: Select all

if (checkvarname($iterate[0])) {
		$code = "\${$iterate[0]}";
		array_shift($iterate);
		foreach ($iterate as $readfile_value) {
			if ($readfile_value == "") {
				$code .= "[]";
			} else {
				$code .= "['$readfile_value']";
			}
		}
		$code .= " = \$data;";
		eval($code);
	}
What I have is an array "$iterate" that defines the key for a variable I'm making. In simple PHP terms, this would be:

Code: Select all

${$iterateї0]}ї$iterateї1]]ї$iterateї2]]ї$iterateї3]]... = $data;
The problem is the "..." I have an arbitrary nesting scheme (the depth of the array varies), and thus I can't use a construct like that to define my array through keys. And thus I've been forced to use eval().

I have a few questions:

1. Is using eval() in this case evil? Is eval() evil in most cases?
2. Am I doing something wrong when I'm organizing my data this way? Should I just start using databases? How do you suggest I store data if I need to make direct changes to the database and them upload them to the site?
3. If this is the correct way of doing it (or semicorrect, for that matter), IS THERE A BETTER WAY?

Thanks. A minor note: Here's a sample of what this might parse:

Code: Select all

$sinf:supercategory:cage>original
$sinf:maincategory:cage>fiction
$sinf:creation:cage>2005.02.13
$sinf:update:cage>2005.02.13
$sinf:sc:s:cage>chtml
$sinf:injectstyle:cage:lyrics>font-style:italic;
$sinf:sc:t:cage>> &quote;їїї&quote; => &quote;font-family:'Monotype 
Corsiva',cursive,serif;font-size:16pt;text-align:center;&quote;,
&quote;===&quote; => &quote;font-style:italic;text-align:center;&quote;
$sc:n:cage:1>Prologue
Creating the corresponding variables into the global space. There are three variables that are operated on: $sinf, $sc and $authinfo (which isn't displayed)


feyd | hey look ma,

Code: Select all

is online. [/color]

Posted: Wed Mar 09, 2005 8:41 pm
by feyd
  1. eval() in most cases is indeed evil. Unless carefully crafted, the code sent to it is quite fragile in nature very often.
  2. a database may be a better direction to head. Although it's difficult to tell what you're trying to do with your data file at this point.
  3. The only other way I can recommend is a more straight forward one, where you have a file that builds a single array of the data you need from this. Kinda like a cached version of what you generate right now.

Posted: Wed Mar 09, 2005 8:48 pm
by Ambush Commander
a database may be a better direction to head. Although it's difficult to tell what you're trying to do with your data file at this point.
Hmm... See, databases use tables, and I'm not exactly sure how to recreate what I'm doing right now in that. The idea about the code is that it creates this big array, $sinf, with lots and lots of information. Now, normally, that wouldn't be a problem, but as you can see from the sample, the value is mixed. I don't think you can put arrays in MySQL tables.

Furthermore, I only have one database provided by my webhost, so this might require a lot of tables. However, a database does sound like a good idea.

Should databases be used for what I'm trying to do? I'm thinking maybe not, because the way I update this information is manually, by hand, I add new values. That's clunky with databases, right? Sigh... guess I'm going to have to automate my stuff. How would you suggest storing arrays in a database?

Posted: Wed Mar 09, 2005 9:24 pm
by feyd
I would imagine it's possible to set the variables via references without much issue..

Posted: Wed Mar 09, 2005 9:26 pm
by Ambush Commander
And it all comes back to me thinking that I could pass any arbitrary variable. ::Sighs:: If you have a lot of variables, how do you logically organize all the references?

Posted: Wed Mar 09, 2005 9:30 pm
by feyd
you don't. I was referring to the assignment of the variables.. so you could remove the use of eval() ;)

Posted: Wed Mar 09, 2005 9:33 pm
by Ambush Commander
Hmm, so instead of that code, we could have this?

Code: Select all

if (checkvarname($iterate[0])) {
        $code = "\${$iterate[0]}";
        array_shift($iterate);
        foreach ($iterate as $key => $readfile_value) {
            if ($readfile_value == "") {
                $code .= "[]";
            } else {
                $code .= "[\$iterate[\$key]]";
            }
        }
        $code .= " = \$data;";
        eval($code);
    }
In this manner, nothing foreign is being passed into eval, and I think it would be pretty clean. Is this what you meant? Is there a performance overhead for eval()?

Posted: Wed Mar 09, 2005 9:42 pm
by feyd

Code: Select all

$code =& ${$iterateї0]};
array_shift($iterate);
foreach($iterate as $key)
{
  if($key !== '')
  {
    if(!isset($codeї$key]))
    {
      $codeї$key] = array();
    }
    $code =& $codeї$key];
  }
  else
  {
    $codeї] = $data;
  }
}

Posted: Wed Mar 09, 2005 9:58 pm
by Ambush Commander
Ohhhh!!!! It suddenly... makes sense! Wow. References are powerful. That works. That works very well. Actually, I might have to tweak it a little, but it works great nonetheless. Niceness.

Of course, it's probably not the best way to do it (I mean, the overall idea of creating your own data format for storing values).

Posted: Wed Mar 09, 2005 10:03 pm
by feyd
yeah.. as I said earlier.. the other direction I'd suggest is just creating a php parseable file that sets up all the data array stuff explicitly.. so it's less confusing to anyone who comes along after you to maintain this. :)

Posted: Wed Mar 09, 2005 10:07 pm
by Ambush Commander
Well... wben I migrated it over (yes, it used to be PHP files), I thought that converting them two text files would:

One, stop errors from happening (the new version has builtin checks to stop errors when parsing it.

Two, to keep things simpler to maintain. I feel nervous when I'm editing PHP files using a script. Doesn't seem like... the right thing to do.