Forum Moderators: coopster

Message Too Old, No Replies

Stuck on looping in an array with foreach

         

henry14

1:55 am on Jan 3, 2008 (gmt 0)

10+ Year Member



I constructed the following script:

Guild <?php echo $_GET["name"];?>.<br />
<?php
$character = $_GET["name"];
$url = 'http://www.example.com?subtopic=guilds&page=view&GuildName=';
$url2 = ($url.$character);
$page = file_get_contents($url2);
$pattern = '/&name=(.+)"/';
preg_match_all($pattern, $page, $matches, PREG_PATTERN_ORDER );

$url = 'http://www.example.com?subtopic=characters&name=';?>

This obviously constructs a URL from 2 variables from a form, and creates a string variable of the URL.

I then filter through the variable $page to find the data I am looking for (Names of all characters in a guild)

My problem is from the source text, I get some values in my array that I don't want and some that I do..

The variables in $matches[0][[0] to $matches[0][[17] i do not want and the variables $matches[0][[0] to $matches[1][[17] i do want, from the array.

When I use:

foreach ($matches as $val) {
echo "Character: " . $val[0] . "\n";
}

It starts with all the variables I do not want to use. Therefore I can't work out how I can loop specifically the variables I do need.

Hope someone can help,
thanks alot

henry14

1:57 am on Jan 3, 2008 (gmt 0)

10+ Year Member



Sorry if there's any confusion, I meant:

I only want the variables $matches[1][[0] to $matches[1][[17], from the array.

(made a typing mistake above)

Habtom

10:46 am on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looking for something like this . . .?

foreach ($matches as $val) {
if ($matches[1][[0] == $matches[1][[17]) {
echo "Character: " . $val[0] . "\n";
}
}

henry14

3:43 pm on Jan 3, 2008 (gmt 0)

10+ Year Member



Yeah exactly ;)

Cheers <3

henry14

3:46 pm on Jan 3, 2008 (gmt 0)

10+ Year Member



Although, the section of the array I wish to use is the parenthisized section of my pattern. Which is found at $var[1]
  • The number of variables dynamic (i don't know how many will be in the array)

    Could I do this?

    foreach ($matches as $val) {
    if ($matches[1][[0] == $matches[1][

  • ) {
    echo "Character: " . $val[0] . "\n";
    }
    }

    thanks

  • PHP_Chimp

    4:14 pm on Jan 3, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    if ($matches[1][[0] == $matches[1][[17]) {

    Are you always after the last element of the $matches[1] array?
    As if you are then you could use count($matches[1]) to give you the length of the array.

    $last_match = count($matches[1])-1; // as arrays start from 0
    if ($matches[1][0] == $matches[1][$last_match]) {
    //
    }

    henry14

    11:49 am on Jan 4, 2008 (gmt 0)

    10+ Year Member



    My script works on the principle of searching one page and collecting an array of variables, then searching another page using each of the variables in my previous array as a pattern.

    For example:

    $page = file_get_contents($url2);
    $pattern = '/>(.[a-z]+)<\/A>/';
    preg_match_all($pattern, $page, $matches, PREG_SET_ORDER );

    Gets the array of variables I want, which works fine.

    Then I try the following:

    $page2 = file_get_contents($url3);
    $pattern2 = '/$bal[1]/';
    foreach ($matches[14][0] as $val) {
    preg_match_all($matches, $page2, $matches2, PREG_SET_ORDER );
    print_r ($matches2);
    }

    I get an error saying that I can't use arrays and must use strings in my preg_match_all function.

    So, I don't know how to go through each variable in my first array, and pregmatch them to anything on the next page. (the number of variables in my first array is not constant and also, the only variables I want in my first array are:

    $matches[1][0], $matches[2][0], $matches[3][0], $matches[4][0] etc..

    Any help is appreciated, this is driving me crazy! thanks

    d40sithui

    3:44 pm on Jan 4, 2008 (gmt 0)

    10+ Year Member



    i think i see why ur getting the preg_match_all error. the pattern should be a string, but in your case, you have an array in its place.

    replace this:
    foreach ($matches[14][0] as $val) {
    preg_match_all($matches, $page2, $matches2, PREG_SET_ORDER );
    print_r ($matches2);
    }

    with:
    foreach ($matches[14][0] as $val) {
    preg_match_all($val, $page2, $matches2, PREG_SET_ORDER );
    print_r ($matches2);
    }

    henry14

    3:35 am on Jan 7, 2008 (gmt 0)

    10+ Year Member



    I tried all that was suggested but it doesnt fit what I wish to do.

    I can collect 2 arrays of Data. The first away, holds the data (collection of names) i need in $matches[X][1] (where X is any number between 0 and the last element).

    The second array holds many names, some of which are also in array 1.

    I am trying desperately to find a way to loop, for every name in array 1, check to see if this name is in array 2.

    I have had so much trouble with foreach, I have just tried to do so with an If statement for now..

    I have:

    $t = 1;
    if ($matches[3][1] == $matches2[$t][1])
    {
    $t++;
    echo "online";
    }

    I couldn't get this to work but hopefully it demonstrates the princple of what i am trying to do, to someone who knows what to do!

    cheers

    Habtom

    3:41 am on Jan 7, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    I am trying desperately to find a way to loop, for every name in array 1, check to see if this name is in array 2.

    Seems you can do it with array_intersect [php.net]

    $result = array_intersect($array1, $array2)

    henry14

    4:32 am on Jan 7, 2008 (gmt 0)

    10+ Year Member



    thanks :)

    henry14

    6:49 am on Jan 7, 2008 (gmt 0)

    10+ Year Member



    When I do the intersect function, it just gives an array which is identical to the first e.g. ($matches) in:

    print_r(array_intersect($matches, $online));

    This is everything I have written:

    Guild <?php echo $_GET["name"];?>.<br />
    <?php
    $guild = $_GET["name"];
    $guild = preg_replace('/(\ )/','+',$guild);
    $url = 'http://www.example.com';
    $url2 = ($url.$guild);
    $page = file_get_contents($url2);
    $pattern = '/&name=([^0-9]+)"/';
    preg_match_all($pattern, $page, $matches, PREG_SET_ORDER );
    print_r ($matches);

    foreach ($matches as $bal) {
    echo "<table border=\"1\">";
    echo "<tr><th>$bal[1]</th>";
    }

    $url3 = 'http://www.example2.com';
    $page2 = file_get_contents($url3);
    $pattern2 = '/(&name=)([^0-9]+)"/';
    preg_match_all($pattern2, $page2, $online, PREG_SET_ORDER );
    //print_r ($online);
    print_r(array_intersect($matches, $online));

    I can't see what is wrong here, that the 2 array's wont intersect. I checked using var_dump() that each array was in strings.

    Habtom

    7:03 am on Jan 7, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    I couldn't check everything, but just before the following line:

    print_r(array_intersect($matches, $online));

    print $matches and $online and see if they have anything in common.

    henry14

    2:28 pm on Jan 7, 2008 (gmt 0)

    10+ Year Member



    Yeah, there are elements in the array with the same value.

    PHP_Chimp

    8:19 pm on Jan 7, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    How does this fit with what you are after -

    $names = array('george', 'fred', 'bill', 'harry', 'ron');
    $others = array('harry', 'ron', 'ginny');
    $matching = array();
    foreach ($names as $name) {
    foreach ($others as $other) {
    if ($name = $other) {
    $matching[] = $name;
    }
    }
    }

    This should loop through everyone in both arrays and find matching values. Then assign the matching value to the $matching array.