English 中文(简体)
• 如何在“单一”“前程”中使用多个阵列
原标题:How to use multiple arrays in " single " foreach() loop [duplicate]

我试图用单一陶器的ach子来补充两个阵列的所有变量。 一些编码实例将获得荣誉。

我有:

<?php
foreach($menu_names as $menu_name){
echo "<li><a href= ?subj= " .urlencode($subjects["id"]). "  >".$menu_name."</a></li>";
}
?>

我想在这个大厅内再增加一个阵列。

最佳回答

假设这两个阵列中的项目数目相同。

if you want to use foreach(), then the arrays need to have the same index:

foreach ($a as $index => $value)
{
    echo $a[$index] .   . $b[$index];
}

如果阵列有数字指数,你只能使用<代码>(>):

for ($i = 0; $i < sizeof($a); $i++)
{
    echo $a[$i] .   . $b[$i];
}
问题回答

array_combine() will merge your 2 arrays into one with key=>value pairs and then access them through foreach statement e.g

foreach(array_combine($a,$b) as $key=>$value) {
    echo $key."<br/>".$value;
}

答案取决于实际。 你们需要与阵列和你们需要怎样做。

如果贵国的阵列有类似的指数,而且你需要使用每一阵列中的相同成分,那么你可以这样做。

foreach ($array1 as $index => $value1) {
    $value2 = $array2[$index];
    // do stuff with $value1 and $value2 here
}

(尽管在此情况下,特别是如果你发现自己做了很多工作,你可能想使用单一系列的物体或阵列,以便数据始终齐头并易于连接。)

或者,如果这些阵列有相同类型的内容,而且你希望对这两者进行重复,你就可以在<条码>上重复(第1款,第1款,第2款)。 (如果阵列在数字上加以指数化,但特别是如果它们具有同样的强力,则阵列要素之一可以取代。) See the docs on array_joint for details.

还有其他一些可能性,取决于你们如何需要这些要素。 (问题确实没有为此提供任何信息)。

正式法典:

1 P-4, 1 P-3, 1 FS, 1 NS

<?php
$First = array( a ,  b ,  c ,  d );
$Second = array( 1 ,  2 ,  3 ,  4 );

foreach($First as $indx => $value) {
    echo $First[$indx].$Second[$indx];
    echo "<br />";
}
?>

<?php
$First = array( a ,  b ,  c ,  d );
$Second = array( 1 ,  2 ,  3 ,  4 );

for ($indx = 0 ; $indx < count($First); $indx ++) {
  echo $First[$indx] . $Second[$indx];
  echo "<br />";
}
?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签