Jump to content

foreach


supermerc

Recommended Posts

Could someone explain to me how foreach works?

 

I have a curl script that gets a page then with the page I do a preg_match_all to find some data

 

preg_match_all('~<td style="padding-left:25px;" align="left">Sigil</td>\s*<td><a href="profile\.php\?id=([^"]+)">([^<]*)<~', $data, $matches);

 

I get data in $matches[1] and $matches[2]

 

And I want to do a foreach statement so separate them all so for each pair of 1 and 2 I can do something with them.

 

So if someone could explain me how it works. I tried:

 

foreach($matches as $array) {

foreach($array as $match) {

 

and i got weird letters and numbers as my variables and it wasnt the case,

 

then I tried:

 

foreach($matches as $k=>$match)

 

And it got me something else that was weird

 

so if someone could explain it to me so I finally understand how it works it would help a lot.

 

 

Link to comment
Share on other sites

I did do that

 

Array ( [0] => Array ( [0] => Sigil BabyBrutes<  [1] => Sigil PotHunting<  [2] => Sigil PotHunting1<  [3] => Sigil PotHuntingFIRE<  [4] => Sigil PotArcane1<  [5] => Sigil PotArcane2<  [6] => Sigil PotHoly<  [7] => Sigil babykillerv6<  [8] => Sigil PotKinetic2<  [9] => Sigil PotKinetic<  [10] => Sigil PotShadow2<  [11] => Sigil PotShadow1<  [12] => Sigil PotShadow<  [13] => Sigil PotHoly2<  [14] => Sigil PotHoly1<  [15] => Sigil PotKinetic1<  [16] => Sigil bloodyslet<  [17] => Sigil Bloodieke<  [18] => Sigil Weezer<  [19] => Sigil Bruce<  [20] => Sigil xKiLLeR13x<  [21] => Sigil BabyGovernment<  [22] => Sigil BabyCobalt<  [23] => Sigil BabyDest<  [24] => Sigil BabyLifeforces<  [25] => Sigil BabySoulgems<  [26] => Sigil BabyNoc<  [27] => Sigil babykillerv4<  [28] => Sigil babykillerv5<  [29] => Sigil BabyCrewstones<  ) [1] => Array ( [0] => 22829 [1] => 24039 [2] => 24040 [3] => 24041 [4] => 24044 [5] => 24046 [6] => 24051 [7] => 99876 [8] => 24061 [9] => 24059 [10] => 24057 [11] => 24056 [12] => 24054 [13] => 24053 [14] => 24052 [15] => 24060 [16] => 6380 [17] => 6381 [18] => 6394 [19] => 6396 [20] => 6650 [21] => 10769 [22] => 8407 [23] => 10771 [24] => 10773 [25] => 10775 [26] => 10774 [27] => 99874 [28] => 99875 [29] => 10832 ) [2] => Array ( [0] => BabyBrutes [1] => PotHunting [2] => PotHunting1 [3] => PotHuntingFIRE [4] => PotArcane1 [5] => PotArcane2 [6] => PotHoly [7] => babykillerv6 [8] => PotKinetic2 [9] => PotKinetic [10] => PotShadow2 [11] => PotShadow1 [12] => PotShadow [13] => PotHoly2 [14] => PotHoly1 [15] => PotKinetic1 [16] => bloodyslet [17] => Bloodieke [18] => Weezer [19] => Bruce [20] => xKiLLeR13x [21] => BabyGovernment [22] => BabyCobalt [23] => BabyDest [24] => BabyLifeforces [25] => BabySoulgems [26] => BabyNoc [27] => babykillerv4 [28] => babykillerv5 [29] => BabyCrewstones ) )

Link to comment
Share on other sites

To do a foreach you must know the data residing in your arrays therefore you need to start var_dump()ing your array to know the structure.

 

foreach iterates an array, iterates = lets say there are 8 keys with values stored in an array so foreach goes from 0 to 7 (do note that arrays always starts at 0 unless its a custom one)

 

supposing we have

$array = array('1','2','3','4'); //The count would be 3 
var_dump($array);

in an array a key starts with zero (yes I'm repeating myself)

 

So, imagine this

 

0 = 1

1 = 2

2 = 3

3 = 4

 

Now when we want to do a foreach

 

foreach($array as $key => $value) {
// foreach is a loop that will stop at the end of an array
echo "Key: ".$key ".PHP_EOL;
echo "Value: ".$value."<br />".PHP_EOL;
}

 

Link to comment
Share on other sites

As suggested by flyhoney - print_r will show you the structure of the array so you have an idea of what foreach is running through.

 

Let's take a single dimensional array as an example, one containing a list of 5 names (adam, peter, paul, robert, jake)

$arrNames=array('adam','peter','paul','robert','jake');

 

Now we'll use foreach to run through the array sending some text to the browser:

foreach ($arrNames as $name) {
  echo 'Hello '.$name.'<br>';
}

 

As you can see foreach runs through the array passing the elements in turn as $name and is the same as:

for ($i=0;$i<count($arrNames);++$i) {
  echo 'Hello '.$arrNames[$i].'<br>';
}

 

Let's say we have an indexed array - the indexes are the names and the values are their age:

$arrAges=array('adam' => 18,'peter' => 21,'paul' => 35,'robert' => 65,'jake' => 14);

 

Now we can use foreach to run through each element accessing the index and value:

foreach ($arrAges as $key => $value) {
  echo $key.' is '.$value.' years old<br>';
}

 

Home that helps.

Link to comment
Share on other sites

And I want to do a foreach statement so separate them all so for each pair of 1 and 2 I can do something with them.

 

You can add a fourth parameter to preg_match_all(), PREG_SET_ORDER, to order the subpatterns together in arrays. Then loop through each set:

 

<?php
foreach ($matches as $array) {
//$array[1] is the grabbed ID
//$array[2] is the grabbed corresponding name
}
?>

Link to comment
Share on other sites

And I want to do a foreach statement so separate them all so for each pair of 1 and 2 I can do something with them.

 

You can add a fourth parameter to preg_match_all(), PREG_SET_ORDER, to order the subpatterns together in arrays. Then loop through each set:

 

<?php
foreach ($matches as $array) {
//$array[1] is the grabbed ID
//$array[2] is the grabbed corresponding name
}
?>

 

Can you elaborate on "You can add a fourth parameter to preg_match_all(), PREG_SET_ORDER, to order the subpatterns together in arrays." PLease

Link to comment
Share on other sites

Use

 

preg_match_all('~<td style="padding-left:25px;" align="left">Sigil</td>\s*<td><a href="profile\.php\?id=([^"]+)">([^<]*)<~', $data, $matches, PREG_SET_ORDER);

and then use the foreach loop I posted to loop through each set of IDs/names.

 

Edit: Matthew beat me to it..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.