foreach循环有4个数组

问题描述 投票:0回答:6

我需要使用带有4个阵列的for each用于学校项目,但我找不到某个地方的解决方案。有没有办法使用for each使用4 arrays循环。我尝试了很多东西,但对我没用。

<?php
  $Voornamen = array( '0300013' => "Laurence" , '0266931' => "Peter" , '0267730' => "Pim" , '0279410' => "Arben" , '0297652' => "Robbin" , '0300838' => "Damian");
  $Achternamen = array( '0300013' => "Van der Wel" , '0266931' => "Kuipers" , '0267730' => "Hoomans" , '0279410' => "Hajrizaj" , '0297652' => "Visser" , '0300838' => "Trojak");
  $Woonplaatsen = array( '0300013' => "Enschede" , '0266931' => "Enschede" , '0267730' => "Enschede" , '0279410' => "Enschede" , '0297652' => "Overdinkel" , '0300838' => "Enschede");
  $Leeftijden = array( '0300013' => "17" , '0266931' => "20" , '0267730' => "20" , '0279410' => "19" , '0297652' => "16" , '0300838' => "16");

  $persoon = array_rand($voornaam, 1);

  foreach ($Voornamen as $Voornaam and $Achternamen as $Achternaam and $Woonplaatsen as $Woonplaats and $Leeftijden as $Leeftijd){
      echo "<table>
        <tr>
          <td>
            $Voornaam
            </td>
        </tr>
        <tr>
          <td>
            $Achternaam
          </td>
        </tr>
        <tr>
          <td>
            $Woonplaats
          </td>
        </tr>
        <tr>
          <td>
            $Leeftijd
          </td>
        </tr>
      </table>";
  }


?>
php arrays foreach
6个回答
0
投票

更改数据结构将是一个很好的解决方案,但如果您必须坚持使用,您可以使用此代码

foreach ($Voornamen as $id => $value){

      echo "<table>
        <tr>
          <td>
            ".$value."
            </td>
        </tr>
        <tr>
          <td>
            " . ($Achternamen[$id]) . "
          </td>
        </tr>
        <tr>
          <td>
            ".($Woonplaatsen[$id])."
          </td>
        </tr>
        <tr>
          <td>
            ".($Leeftijden[$id])."
          </td>
        </tr>
      </table>";
  }

0
投票

你可以逐个遍历每一个:

foreach($array1 as $key1 => $value1){
    foreach($array2 as $key2 => $value2){
        ... etc

但这可能会变得相当混乱。你真的必须使用4个阵列吗?


0
投票

我尝试将你的所有数组放入一个数组并创建一个函数来显示这里的所有名称是代码:

$Voornamen = array( '0300013' => "Laurence" , '0266931' => "Peter" , '0267730' => "Pim" , '0279410' => "Arben" , '0297652' => "Robbin" , '0300838' => "Damian");
  $Achternamen = array( '0300013' => "Van der Wel" , '0266931' => "Kuipers" , '0267730' => "Hoomans" , '0279410' => "Hajrizaj" , '0297652' => "Visser" , '0300838' => "Trojak");
  $Woonplaatsen = array( '0300013' => "Enschede" , '0266931' => "Enschede" , '0267730' => "Enschede" , '0279410' => "Enschede" , '0297652' => "Overdinkel" , '0300838' => "Enschede");
  $Leeftijden = array( '0300013' => "17" , '0266931' => "20" , '0267730' => "20" , '0279410' => "19" , '0297652' => "16" , '0300838' => "16");

  //$persoon = array_rand($voornaam, 1);

  $arr = array($Voornamen,$Achternamen,$Woonplaatsen,$Leeftijden);
multi_array($arr);
function multi_array($array){
  foreach ($array as $key => $val){
      foreach($val as $v) {
        echo "<table>
            <tr>
              <td>
                $v
                </td>
            </tr>               
          </table>";
      }
  }
}

0
投票

使用foreach迭代数组;打印该值并使用square brackets syntax中的键访问其他数组中的相应值:

$Voornamen = array( '0300013' => "Laurence" , '0266931' => "Peter" , '0267730' => "Pim" , '0279410' => "Arben" , '0297652' => "Robbin" , '0300838' => "Damian");
$Achternamen = array( '0300013' => "Van der Wel" , '0266931' => "Kuipers" , '0267730' => "Hoomans" , '0279410' => "Hajrizaj" , '0297652' => "Visser" , '0300838' => "Trojak");
$Woonplaatsen = array( '0300013' => "Enschede" , '0266931' => "Enschede" , '0267730' => "Enschede" , '0279410' => "Enschede" , '0297652' => "Overdinkel" , '0300838' => "Enschede");
$Leeftijden = array( '0300013' => "17" , '0266931' => "20" , '0267730' => "20" , '0279410' => "19" , '0297652' => "16" , '0300838' => "16");

$persoon = array_rand($voornaam, 1);

foreach ($Voornamen as $id => $Voornaam) {
    $Achternaam = $Achternamen[$id];
    $Woonplaats = $Woonplaatsen[$id];
    $Leeftijd   = $Leeftijden[$id];
?>
    <table>
    <tr>
        <td><?php echo(htmlspecialchars($Voornaam)); ?></td>
    </tr>
    <tr>
        <td><?php echo(htmlspecialchars($Achternaam)); ?></td>
    </tr>
    <tr>
        <td><?php echo(htmlspecialchars($Woonplaats)); ?></td>
    </tr>
    <tr>
        <td><?php echo(htmlspecialchars($Leeftijd)); ?></td>
    </tr>
    </table>
<?php } ?>

更好的方法是将同一个人的所有属性保存在单个associative array中,并且仅保留包含由其ID标记的所有人的列表:

$persons = array(
    '0300013' => array(
        'voornaam'   => 'Laurence',
        'achternaam' => 'Van der Wel',
        'woonplaats' => 'Enschede',
        'leeftijd'   => '17',
    ),
    '0266931' => array(
        'voornaam'   => 'Peter',
        'achternaam' => 'Kuipers',
        'woonplaats' => 'Enschede',
        'leeftijd'   => '20',
    ),
    '0267730' => array(
        'voornaam'   => 'Pim',
        'achternaam' => 'Hoomans',
        'woonplaats' => 'Enschede',
        'leeftijd'   => '20',
    ),
    '0279410' => array(
        'voornaam'   => 'Arben',
        'achternaam' => 'Hajrizaj',
        'woonplaats' => 'Enschede',
        'leeftijd'   => '19',
    ),
    '0297652' => array(
        'voornaam'   => 'Robbin',
        'achternaam' => 'Visser',
        'woonplaats' => 'Overdinkel',
        'leeftijd'   => '16',
    ),
    '0300838' => array(
        'voornaam'   => 'Damian',
        'achternaam' => 'Trojak',
        'woonplaats' => 'Enschede',
        'leeftijd'   => '16',
    ),
);

foreach ($persons as $p) { ?>
    <table>
    <tr>
        <td><?php echo(htmlspecialchars($p['voornaam'])); ?></td>
    </tr>
    <tr>
        <td><?php echo(htmlspecialchars($p['achternaam'])); ?></td>
    </tr>
    <tr>
        <td><?php echo(htmlspecialchars($p['woonplaats'])); ?></td>
    </tr>
    <tr>
        <td><?php echo(htmlspecialchars($p['leeftijd'])); ?></td>
    </tr>
    </table>
<?php } ?>

0
投票

目前还不清楚最终的意图,所以我认为最终目标是在表格中显示数组中的数据?

如上所述,你只能向foreach提供一个数组,但是你可以,因为源数组的长度相同,所以使用计数器来允许访问其他数组。

$Voornamen = array( '0300013' => "Laurence" , '0266931' => "Peter" , '0267730' => "Pim" , '0279410' => "Arben" , '0297652' => "Robbin" , '0300838' => "Damian");
$Achternamen = array( '0300013' => "Van der Wel" , '0266931' => "Kuipers" , '0267730' => "Hoomans" , '0279410' => "Hajrizaj" , '0297652' => "Visser" , '0300838' => "Trojak");
$Woonplaatsen = array( '0300013' => "Enschede" , '0266931' => "Enschede" , '0267730' => "Enschede" , '0279410' => "Enschede" , '0297652' => "Overdinkel" , '0300838' => "Enschede");
$Leeftijden = array( '0300013' => "17" , '0266931' => "20" , '0267730' => "20" , '0279410' => "19" , '0297652' => "16" , '0300838' => "16");

$kv=array_keys( $Voornamen );
$ka=array_keys( $Achternamen );
$kw=array_keys( $Woonplaatsen );
$kl=array_keys( $Leeftijden );

$i=0;


echo "<table cellspacing='5px' cellpadding='10px' border='1'>";

foreach( $Voornamen as $key => $value ){

    echo "
    <tr>
        <td>{$key}:{$value}</td>
        <td>{$ka[ $i ]}:{$Achternamen[ $ka[ $i ] ]}</td>
        <td>{$kw[ $i ]}:{$Woonplaatsen[ $kw[ $i ] ]}</td>
        <td>{$kl[ $i ]}:{$Leeftijden[ $kl[ $i ] ]}</td>
    </tr>";

    $i++;
}

echo "</table>";

这会输出像这样的HTML Table


0
投票

是的,因为所有阵列都具有相同的键和大小:

foreach ($Voornamen as $key => $value){
  echo $Voornamen[$key];// or just $value
  echo $Achternamen[$key];
  echo $Woonplaatsen[$key];
   echo $Leeftijden[$key];
}

另一种方法是从所有数组构造一个多维数组,并使用两个嵌套的FOREACH循环。 ............

我向你的老师致以问候。

© www.soinside.com 2019 - 2024. All rights reserved.