Bamuel Bamuel - 2 months ago 10
PHP Question

2 PHP foreach (arrays) into one

I am currently having issues with these 2 foreach

$caughtpkmnfile = file ('save/' . $username . '/Pokemon.txt');
foreach ($caughtpkmnfile as $caught2) {
$caughtpokemon2 = explode('|', $caught2);
foreach ($caughtpokemon2 as $pkmnc){
if ($pkmnc == ""){
continue;
}
echo '<img src="pokemonsprites/' . $pkmnc . '.gif" id="pkmnc"/><p style="margin-left: 45px">' . $pkmnc . '</p> <br>';
}
}


&

foreach (range(1, 718) as $number) {
$pkmn2 = new \PokemonAPI\Pokemon($number);
$pkmn = $pkmn2->getName();
echo '<img src="pokemonsprites/' . $pkmn . '.gif" id="pkmn"/><p style="margin-left: 45px">' . $pkmn . '</p> <br>';
ob_flush();
flush(); //ie working must
}


With these two arrays, focusing on the second array, it outputs all the pokemon (images) out, and the first foreach code does the same thing but with caught pokemon (Pokemon from saved file), The output i get it
image, note that in .css the second code img#pkmn has no brightness in image, while first has brightness.

I am trying to make it list, in the same numeric order but if it corresponds with the caught it does id=pkmnc


not working example

if ($pkmn == $pkmnc){
echo '<img src="pokemonsprites/' . $pkmnc . '.gif" id="pkmnc"/><p style="margin-left: 45px">' . $pkmnc . '</p> <br>';
}
else{
echo '<img src="pokemonsprites/' . $pkmn . '.gif" id="pkmn"/><p style="margin-left: 45px">' . $pkmn . '</p> <br>';
}


^^that needs to played in the foreach
In the Pokemon.txt

Chespin|Aron|Glalie|Luxio|Flareon|

Answer

Here is a working possibility for a solution to your problem using one loop:

// Start question askers code
$pokemonFileContents = file_get_contents("Pokemon.txt");

foreach (range(1, 718) as $number) {
    $pkmn2 = new \PokemonAPI\Pokemon($number); 
    $pkmn = $pkmn2->getName();

    // The regex checks if it matches the name
    // with a | infront or the name with a | at the end
    //
    // Also if we have a file with no | characters it is 
    // possible to have just one pokemon name
    // this is what the or checks for
    //

    $id = "pkmn";
    if (preg_match("/(\|$pkmn)|($pkmn\|)/", $pokemonFileContents) || $pokemonFileContents == $pkmn) {

        $id .= "c";
    } 

    // I've done the if differently but there is nothing wrong
    // with doing it the other way if you feel it is more readable
    // Here I am just building up the id attribute so I can have one echo

    echo '<img src="pokemonsprites/' . $pkmn . 
        '.gif" id="'.$id.'"/><p style="margin-left: 45px">' . $pkmn . '</p> <br>'."\n";


    ob_flush();
    flush(); //ie working must
}

Note you must pull the file contents using file_get_contents so you have the contents as a string.

This code is an excerpt from this Github project here: https://github.com/davethomas11/stackoverflow_Q_39784013

And in case you want a solution with no regex, this would work as well:

$caughtpkmnfile = file ('save/' . $username . '/Pokemon.txt');
$caughtpokemon2 = [];
foreach ($caughtpkmnfile as $caught2) {
    $caughtpokemon2 = array_merge(explode('|', $caught2), $caughtpokemon2);
}

foreach (range(1, 718) as $number) {
    $pkmn2 = new \PokemonAPI\Pokemon($number); // Note: *warning I removed namespace, don't forget to put it back
    $pkmn = $pkmn2->getName();

    // The regex checks if it matches the name
    // with a | infront or the name with a | at the end
    //
    // Also if we have a file with no | characters it is
    // possible to have just one pokemon name
    // this is what the or checks for
    //

    $id = "pkmn";
    if (in_array($pkmn, $caughtpokemon2)) {

        $id .= "c";
    }

    echo '<img src="pokemonsprites/' . $pkmn .
        '.gif" id="'.$id.'"/><p style="margin-left: 45px">' . $pkmn . '</p> <br>'."\n";



    ob_flush();
    flush(); //ie working must
}

When benchmarked against the regex code, this code is 2x faster. Note because of the number of iterations being done it does not matter significantly.

Using microtime() ->

With regex run time: 0.011636 seconds

With out regex run time: 0.005497 seconds