2 PHP将foreach(数组)合二为一

时间:2016-09-30 05:02:10

标签: php arrays 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
}

使用这两个数组,专注于第二个数组,它输出所有的口袋妖怪(图像),第一个foreach代码做同样的事情,但有捕获的口袋妖怪(口袋妖怪来自保存的文件),输出我得到它 image,请注意在.css第二个代码img#pkmn在图像中没有亮度,而第一个有亮度。

我正在尝试按照相同的数字顺序列出它,但如果它与捕获的对应,那么id = pkmnc
不工作的例子

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>';
    }

^^需要在foreach中玩 在Pokemon.txt

Chespin|Aron|Glalie|Luxio|Flareon|

2 个答案:

答案 0 :(得分:1)

如果我正确地理解了你的问题,我会将它们组合成第一个数组的键和第二个数值。并且每次使用一个我首先测试$ key,这是第一个数组值,否则是$ value,这是第二个数组值。

   $caughtpokemon2 = array();
   foreach($caughtpkmnfile as $caught2){
   $splitted = explode('|', $caught2);
   array_push($caughtpokemon2,$spliited);
  } 
    foreach($rangeArray as $pkmn)
    {  
    if(in_array($pkmn,$pkmnc)){  //If $pkmn value exists in $pkmnc
     //Find Index of value
       $index = array_search($pkmn,$pkmnc,true);
     echo '<img src="pokemonsprites/' . $pkmnc[$index ]. '.gif" id="pkmnc"/><p     style="margin-left: 45px">' . $pkmnc[$index ]. '</p> <br>';
     } 
    else{
     echo '<img src="pokemonsprites/' . $pkmn. '.gif" id="pkmn"/><p style="margin-left: 45px">' . $pkmn. '</p> <br>';
     }  
    }

答案 1 :(得分:1)

以下是使用一个循环解决问题的可行方法:

// 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
}

请注意,您必须使用file_get_contents提取文件内容,以便将内容作为字符串。

  

此代码摘自此Github项目:   https://github.com/davethomas11/stackoverflow_Q_39784013

如果您想要一个没有正则表达式的解决方案,这也可以使用:

$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
}

在对正则表达式代码进行基准测试时,此代码的速度提高了2倍。请注意,由于迭代次数正在完成,因此无关紧要。

  

使用microtime() - &gt;

     

使用正则表达式运行时间:0.011636秒

     

使用正则表达式运行时间:0.005497秒