
foreach - problems understanding
In a fit of e{*filter*}ment on Fri, 08 Nov 2002 14:12:39 -0500, Dan Rubin
[snip]
| Now though it appears that I still need to figure out one piece of the
| puzzle: How do I access the individual parts of the array?
|
| For instance (and I hope I'm explaining this properly), in my non-PHP mind,
| I thought something like this should work to store each part of the array in
| its own variable (there are only 6 matches per line):
|
| $v1 = $out[1];
| $v2 = $out[2];
| $v3 = $out[3];
| $v4 = $out[4];
| $v5 = $out[5];
| $v6 = $out[6];
|
|
| Which, I reasoned, should allow me to then reference some (or all) of those
| variables below:
|
| echo "$v1: $v2 (Bit of info: $v6)<br />";
|
|
| But I'm way off, since this returns:
|
| Array: Array (Bit of info: Array)
|
| Which leads me to believe I've done something wrong :-)
From the above, it'd appear that you don't really need to add the $vn
variables, as later down the line you can always do...
<?
// ...
print $out[1] . ': ' . $out[2] . ' (Bit of info: ' . $out[6] . ")<br
/>\n";
?>
While the above works fine, I'm a lil puzzled as to why your theory
failed (as it should work). One thing to point out/remember, arrays
always start with value of 0, so $out[1] is actually the second element
as $out[0] would be the first.
|
| If I can split the parts of the array into separate variables, then I can
| reference them in fun and exciting ways further down the page (and I've
| already looked at the split() function, but I don't think it will work
| because my array doesn't have any delimiters, but feel free to correct me if
| I read it all wrong).
Nope, you're right. Split is used for splitting strings via a delimeter.
Something like:
$string = "rootdir=/home/tk/html"
$array = split('=', $string);
print $array[0]; // Would print rootdir
print $array[1]; // Would print the path...
...etc etc...
I've continued to use the test code I did yesterday to outline the above
points in hopefully a slightly clearer view.
http://www.*-*-*.com/
will produce the output. The top section lists the array in the for()
loop, and then uses my method above to print the info line like you did.
The second part below the seperator, is using the method you have.. $v1
= $out[1] etc etc and both work.
http://www.*-*-*.com/
will show you the source for what the page outputs.
|
| Thanks for any help -- I'm very grateful to everyone for their advice and
| instruction so far, it's helping me grasp PHP in a shorter amount of time
| than if I were stuck on my own :-)
|
| Cheers!
No problem =)
PHP's such a flexible language, it's great! you'll love it..
perserverance(sp?) is definitely the key and so far, your theories seem
to be on the right track for things =)
Regards,
tk
--
+--------------------------+
| digiServ Network |
| Web solutions |
| http://www.*-*-*.com/ |
+--------------------------+
Remove WINDOZE to reply