Page 1 of 1
Weird situation processing a variable with a variable...
Posted: Wed Aug 12, 2009 11:32 pm
by Wolf_22
Forgive me if I end up confusing you guys more than myself, but I'm up against a wall here with the use of a variable that I'm trying to get initialized with the value of an array (if I understand it all correctly). See below:
Code: Select all
foreach($ad as $key=>$val){
$id = $val->ID;
$set = 'today-'.$id;
echo $set;
}
To no surprise to many of you, it's not working and I can't seem to figure out a way to process the $val->ID prior to initializing the variable "$id." I know I'm missing something here (and probably something fundamental), but I just can't see it. Feel free to educate me.
Update: I just naively realized that the right arrow indicates an object...
Re: Weird situation processing a variable with a variable...
Posted: Wed Aug 12, 2009 11:40 pm
by requinix
I, for one, don't know what you're trying to do...
Re: Weird situation processing a variable with a variable...
Posted: Wed Aug 12, 2009 11:53 pm
by Wolf_22
I'm sorry. I have a tendency of looking too deeply into things at times.
Maybe you could tell me what the above is? Is is just shotty code or is there something going on with an object and a variable. I know, I know, it's a horrible way to ask something like this but I can't help it.

Re: Weird situation processing a variable with a variable...
Posted: Thu Aug 13, 2009 12:08 am
by requinix
"today-$val->ID" is a string, but since it uses "s PHP sees the $ and starts looking for a valid variable name. It sees the "val", continues with the "->", and ends with "ID": the whole thing is a variable it can insert.
That line is equivalent to
So if $val->ID is 5 then $test = "today-5".
Your confusion is partly why some programmers like a slightly different syntax:
It's a little more obvious what that's supposed to do.
Re: Weird situation processing a variable with a variable...
Posted: Thu Aug 13, 2009 8:01 am
by Wolf_22
Okay, that clears some of this up. After looking at my code, though, I've come up with another question pertaining to arrays.
An array basically consists of "keys" and "values." Creating an array is pretty simple in that you simply create a variable like so "$var[] = whatever." My question is this: how can I not only insert values, but also, control the keys?
If I have the array "$admins[]" and let's say I wish to assign each admin a name, but for their keys, I would like to assign an ID, how could I do this? The reason I'm confused about doing it is because in the past, I've only needed to do the simple assignment where you give the array a bunch of values. Is there a function appropriate for what I'm wanting to do?