PHP 多维数组使用 for in for 来回显字符串


PHP Multi dimensional array using for within for to echo a string

刚刚在这里碰壁,如果有人可以帮助解决这个问题,将不胜感激。

以防万一有人想知道我想要完成什么,我目前正在练习 PHP,所以下面的代码块不是我试图用于网站的东西。

我正在尝试将$cars[0][0]$cars[1][0]连接起来,以制作一个字符串,上面写着:

"A cool car is the Ferrari F458 Super Car"

并让它跳转到下一个字符串,该字符串将$cars[0][1]$cars[1][1]依此类推,直到echo运行数组项的完整$length

使用以下代码,当前显示的结果为:

echo "A cool car is the " . $cars[0][0] . " " . $cars[1][0] . " Super Car<br />";

这将重复 6 行,然后跳转到下一个数组项[0][1] [1][1]再重复 6 行。

我试图让他们回显 1 行并进入下一行,尝试分离echo并用 echofor($b)行写for($a),然后用echo的其余部分,但结果更糟。我尝试了其他一些迭代,但有些只是无效。尝试在网上查找任何内容,但找不到如此具体的内容。我能够达到的最好的结果是它重复相同的字符串 6 次,然后移动到+1上。

注意:我已经能够在另一个使用关联数组的练习会话中完成此操作,但为了本练习会话,我尝试在不使用关联数组的情况下完成它,只是为了更好地理解使用整数。

<?php //Echoing arrays using integers standard method
$cars = array(array("Ferrari", "Lamborghini", "Aston Martin", "Mercedes",   "Maserati", "McLaren"), array("F458", "Aventador", "one-77", "GT", "GTS", "MP4-12C"));
$lengthOne = count($cars[0]);
$lengthTwo = count($cars[1]);
for($a = 0; $a < $lengthOne; $a++) {
    for($b = 0; $b < $lengthTwo; $b++) {
        echo "A cool car is the " . $cars[0][$a] . " " . $cars[1][$b] . " Super Car<br />";
    }
}
?>
<?php

任何和所有的帮助将不胜感激!

编辑:$b现在< $lengthTwo(虽然结果仍然相同)

编辑:$cars[1][$a]更改为[1][$b](结果现在更加混乱)

使用单个循环并对两个子数组使用相同的索引:

法典:

<?php
  $cars = array(
    array("Ferrari", "Lamborghini", "Aston Martin", "Mercedes",   "Maserati", "McLaren"), 
    array("F458", "Aventador", "one-77", "GT", "GTS", "MP4-12C")
  );
  $length = min (count ($cars[0]), count ($cars[1]));  /* In case the arrays have different lengths. */
  for($a = 0; $a < $length; $a++) {
    echo "A cool car is the " . $cars[0][$a] . " " . $cars[1][$a] . " Super Car'n";
  }

输出:

一辆很酷的车是法拉利F458超级跑车一辆很酷的车是兰博基尼Aventador超级跑车一辆很酷的车是阿斯顿马丁 one-77 超级跑车一辆很酷的车是梅赛德斯GT超级跑车一辆很酷的车是玛莎拉蒂GTS超级跑车一辆很酷的汽车是迈凯轮 MP4-12C 超级跑车
$cars = array(
   array("Ferrari","Lamborghini","Aston Martin","Mercedes","Maserati","McLaren"),
   array("F458","Aventador","one-77","GT","GTS","MP4-12C")
);
foreach($cars[0] as $key => $value)
{
    if (isset($cars[1][$key]))
      echo "A cool car is the ".$value." ".$cars[1][$key]." Super Car.'n";
}
我会

将其添加为评论,但我仍然没有 50 次代表...

第二个 bucle 定义了$b,但您不会在任何地方使用它,因此第二个 bucle 中显示的数据每次运行时都是相同的。

编辑:你既没有在任何地方使用"$lengthTwo"。

本着

学习:)的精神解决同一问题的另外四种方法:

每种方法

reset($cars[0]);
reset($cars[1]);
while( ($brand=each($cars[0])) && 
       ($name=each($cars[1])) ) {
    echo "A cool car is the {$brand[value]} {$name[value]} Super Car<br />'n";
}

下一个方法

$brand = reset($cars[0]);
$name = reset($cars[1]);
do {
  echo "A cool car is the $brand $name Super Car<br />'n";
} while ( ($brand=next($cars[0])) && ($name=next($cars[1])) );

array_combine方法:

$cars_combined = array_combine($cars[0], $cars[1]);
foreach ( $cars_combined as $brand => $name ) {
  echo "A cool car is the $brand $name Super Car<br />'n";
}

array_walk方法:

array_walk($cars[0], function($brand, $key){
    global $cars;
    echo "A cool car is the {$brand} {$cars[1][$key]} Super Car<br />'n";
});

正如Cheery所指出的,从5.3开始,应该使用使用运算符,而不是global。至少可以说,这绝对更整洁。

array_walk($cars[0], function($brand, $key) use($cars) {
    echo "A cool car is the {$brand} {$cars[1][$key]} Super Car<br />'n";
});