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

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

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

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would imagine it's possible to set the variables via references without much issue..
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you don't. I was referring to the assignment of the variables.. so you could remove the use of eval() ;)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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()?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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;
  }
}
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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. :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
Post Reply