Php 使用for和array_flip模拟多个foreach。有更好的办法吗?

Php 使用for和array_flip模拟多个foreach。有更好的办法吗?,php,foreach,array-flip,Php,Foreach,Array Flip,有没有更好的方法来做这样的事情?这很好,但我认为可能有一条捷径可以得到同样的结果。我想,为了方便地添加更多数组,我可以将它转换为一个函数,并在其外部设置for循环,但仍然可以 只是想知道 <? $array1 = array("test1","test2","test3","test4"); $array2 = array("dest1","test2","dest3","dest4"); for ($i=0; $i<count($array1); $i++) { /

有没有更好的方法来做这样的事情?这很好,但我认为可能有一条捷径可以得到同样的结果。我想,为了方便地添加更多数组,我可以将它转换为一个函数,并在其外部设置for循环,但仍然可以

只是想知道

<?
$array1 = array("test1","test2","test3","test4");
$array2 = array("dest1","test2","dest3","dest4");




    for ($i=0; $i<count($array1); $i++) { //FOR STATEMENT TO GET MULTIPLE ARRAYS REGISTERED AS A FOREACH-STYLE 
    $val1 = $array1[$i]; //VALUES GET
    $val2 = $array2[$i];

    $array1 = array_flip($array1); //ARRAY IS FLIPPED FROM KEY => VALUE TO VALUE => KEY SO THAT THE KEYS CAN BE GET. REMEMBER TO SWITCH IT BACK.
    $array2 = array_flip($array2);

    $key1 = $array1[$val1]; //NOW MUST ITERATE THROUGH USING VALUES ABOVE BECAUSE THE KEYS ARE NO LONGER NUMBERS.
    $key2 = $array2[$val2];

    $array1 = array_flip($array1); //SWITCHING IT BACK.
    $array2 = array_flip($array2);

    echo $key1 . "=>" . $val1 . "<br />";
    echo $key2 . "=>" . $val2 . "<br />";

    }




    var_dump($array1);

    ?>

您已经拥有阵列的“键”
$i

for ($i=0; $i<count($array1); $i++) {
    $val1 = $array1[$i]; // You're getting the value from the array,
    $val2 = $array2[$i]; // you already have the key, $i

    echo $i . "=>" . $val1 . "<br />";
    echo $i . "=>" . $val2 . "<br />";
}

目前我能做的最好的

$arrayList = Array($array1, $array2); // Add every array here
$keyArr = Array();
foreach ($arrayList as $key => $value)
{
   // Put the keys of every array in $arrayList in an other array
   $keyArr[$key] = array_keys($value);
}
// Loop while $i is lower than the highest index of the bigest array in $arrayList
for ($i = 0; $i < count(max($arrayList )); $i++)
{
   // Loop every array in $arrayList
   foreach ($arrayList as $arrKey => $arr)
   {
     // If the array in $arrayList has at least $i element
     if ($i < count($arr))
     {

       // Get the key of the $i-n element in the array (take it from the $keyArr defined above)
       $key = $keyArr[$arrKey][$i];
       // Get the value of the $i-n element in the array
       $val = $arr[$key];
     }
   }
}
$arrayList=Array($array1,$array2);//在此处添加每个数组
$keyArr=Array();
foreach($arraylistas$key=>$value)
{
//将$arrayList中每个数组的键放在另一个数组中
$keyArr[$key]=数组_键($value);
}
//循环,而$i低于$arrayList中最大数组的最高索引
对于($i=0;$i$arr)
{
//如果$arrayList中的数组至少有$i元素
如果($i
你到底想做什么?数组的键就是
$i
。哦,对了……实际上,我正试图将它用于没有数字作为键的数组。像(ID=>Person)类型的东西……我完全忘记了数字不会因为这个而起作用,所以我必须将它设为next()和current(),而不是按$I。@user1159454:两个数组的键相同吗?如果是这样,请尝试我添加的第二个示例。这似乎过于复杂。我对其进行了编辑,这是我发现的唯一一种在不操纵键和不循环每个数组的情况下一次“foreach”大量数组的方法。
$arrayList = Array($array1, $array2); // Add every array here
$keyArr = Array();
foreach ($arrayList as $key => $value)
{
   // Put the keys of every array in $arrayList in an other array
   $keyArr[$key] = array_keys($value);
}
// Loop while $i is lower than the highest index of the bigest array in $arrayList
for ($i = 0; $i < count(max($arrayList )); $i++)
{
   // Loop every array in $arrayList
   foreach ($arrayList as $arrKey => $arr)
   {
     // If the array in $arrayList has at least $i element
     if ($i < count($arr))
     {

       // Get the key of the $i-n element in the array (take it from the $keyArr defined above)
       $key = $keyArr[$arrKey][$i];
       // Get the value of the $i-n element in the array
       $val = $arr[$key];
     }
   }
}