foreach - problems understanding 
Author Message
 foreach - problems understanding

OK, I fixed the last problem, and I've now got multiple matches from a line
an an array. I've confirmed their presence using print_r, and now I would
like to output them.

The original method, with one match, uses:

foreach ($out[1] as $link) {
    echo "$link<br />";
    }

And that works perfectly; but when I add the extra matches, the only way I
can output them is by using multiple foreach blocks:

foreach ($out[1] as $link) {
    echo "$link<br />";
    }
    foreach ($out[2] as $othermatch) {
    echo "$othermatch<br />";
    }

And so on.

There *has* to be a cleaner way to do this, but everything I've tried hasn't
worked (granted, my attempts have been guesses, since I can't find anything
yet on PHP.net).

Any suggestions?

Thanks!
--
dan rubin

webgraph: branding | usability | design
< http://www.*-*-*.com/ ;



Wed, 27 Apr 2005 01:30:31 GMT  
 foreach - problems understanding
In a fit of e{*filter*}ment on Fri, 08 Nov 2002 12:30:31 -0500, Dan Rubin

| And that works perfectly; but when I add the extra matches, the only way I
| can output them is by using multiple foreach blocks:
|
| foreach ($out[1] as $link) {
|     echo "$link<br />";
|     }
|     foreach ($out[2] as $othermatch) {
|     echo "$othermatch<br />";
|     }
|
| And so on.
|
| There *has* to be a cleaner way to do this, but everything I've tried hasn't
| worked (granted, my attempts have been guesses, since I can't find anything
| yet on PHP.net).

I'd try something like...

for ($i = 0; $i <= sizeof($out); $i++) {
        foreach ($out[$i] as $link) {
                print $link . "<br />\n";
        }

Quote:
}

HTH.

Regards,

  tk
--
+--------------------------+
|     digiServ Network     |
|      Web solutions       |
| http://www.*-*-*.com/ |
+--------------------------+

  Remove WINDOZE to reply



Wed, 27 Apr 2005 01:33:41 GMT  
 foreach - problems understanding

Quote:

> I'd try something like...

> for ($i = 0; $i <= sizeof($out); $i++) {
> foreach ($out[$i] as $link) {
> print $link . "<br />\n";
> }
> }

> HTH.

Thanks! I'll play with it and see if it works...

Cheers!
--
dan rubin

webgraph: branding | usability | design
<http://www.webgraph.com/>



Wed, 27 Apr 2005 01:56:13 GMT  
 foreach - problems understanding

Quote:

> I'd try something like...

> for ($i = 0; $i <= sizeof($out); $i++) {
> foreach ($out[$i] as $link) {
> print $link . "<br />\n";
> }
> }

OK, when I try this code, I get the following:

 Warning: Invalid argument supplied for foreach()

Any ideas?

Thanks!
--
dan rubin

webgraph: branding | usability | design
<http://www.webgraph.com/>



Wed, 27 Apr 2005 02:22:36 GMT  
 foreach - problems understanding
In a fit of e{*filter*}ment on Fri, 08 Nov 2002 13:22:36 -0500, Dan Rubin


|
| > I'd try something like...
| >
| > for ($i = 0; $i <= sizeof($out); $i++) {
| > foreach ($out[$i] as $link) {
| > print $link . "<br />\n";
| > }
| > }
|
| OK, when I try this code, I get the following:
|
|  Warning: Invalid argument supplied for foreach()
|
| Any ideas?
|
| Thanks!

Oops yeah. How about:

<?
  $out = array("bit1", "bit2", "bit3");

  for ($i = 0; $i <= sizeof($out); $i++) {
          print $out[$i] . "<br />\n";
  }
?>

would appear to work here for me.

Apologies for the complicated (broken) method explained previous.

Regards,

  tk

--
+--------------------------+
|     digiServ Network     |
|      Web solutions       |
| http://www.*-*-*.com/ |
+--------------------------+

  Remove WINDOZE to reply



Wed, 27 Apr 2005 02:49:03 GMT  
 foreach - problems understanding

Quote:

> Oops yeah. How about:

> <?
> $out = array("bit1", "bit2", "bit3");

> for ($i = 0; $i <= sizeof($out); $i++) {
>  print $out[$i] . "<br />\n";
> }
> ?>

> would appear to work here for me.

> Apologies for the complicated (broken) method explained previous.

Ah yes, that does work :-)

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

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

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!
--
dan rubin

webgraph: branding | usability | design
<http://www.webgraph.com/>



Wed, 27 Apr 2005 03:12:39 GMT  
 foreach - problems understanding

Quote:


> > Oops yeah. How about:

> > <?
> > $out = array("bit1", "bit2", "bit3");

> > for ($i = 0; $i <= sizeof($out); $i++) {
> >  print $out[$i] . "<br />\n";
> > }
> > ?>

> > would appear to work here for me.

> > Apologies for the complicated (broken) method explained previous.

> Ah yes, that does work :-)

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

You are close.

Glossary ;)
    match group -- anything in () within the regexp e.g. (.*)
    match       -- an actual substring which matched the pattern

See, the $out[1] is the first match group -- but that is an array of all the
matches.

so $out[1][0]    is the first match group, for the first match
   $out[1][1]    is the first match group, the second match
   $out[1][2]    is the first match group, the third match
   $out[3][0]    is the third match group, the first match

Remember, arrays start indexing at 0, so that is always the 'first'.
preg_match_all stores the entire match in $out[0], so your first match group
is actually stored in $out[1]

So, $out[1][0]
    $out[2][0]
    $out[3][0]
    $out[4][0]
All correspond to the same 'match', just different match groups for that
match ...

So to get all of the matches for match group 4 echo'd to the screen, you
could:

foreach ($out[4] as $link) {
    echo $link;

Quote:
}

To echo all of the match groups for all of the matches you could:

for ($i=1; $i<count($out); $i++) {
    for ($j=0; $j<count($out[$i]); $i++) {
        echo "match group: $i || match number: $j ";
        echo "|| match contents: " . $out[$i][$j];
    }

Quote:
}

regards,
reggie.


Wed, 27 Apr 2005 04:19:52 GMT  
 foreach - problems understanding

Quote:
> for ($i=1; $i<count($out); $i++) {
>     for ($j=0; $j<count($out[$i]); $i++) {

                                      ^^^
                                      ^^^

oops - always one little mistake.

should read $j++

Quote:
>         echo "match group: $i || match number: $j ";
>         echo "|| match contents: " . $out[$i][$j];
>     }
> }

regards,
reggie.


Wed, 27 Apr 2005 04:26:53 GMT  
 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



Wed, 27 Apr 2005 19:31:41 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. foreach problem

2. foreach problem

3. foreach problem

4. foreach var problem

5. Problems i can't understand

6. Help me understand this embed point problem

7. Problems understanding deep_clone clone

8. problem understanding class relationships

9. Problem understanding procedure Adjust(.

10. problem understanding recursion

11. Problem in understanding catch statement

12. Problem understanding some tests in winpipe.test (CVS HEAD)

 

 
Powered by phpBB® Forum Software