PHP用于内部的多维数组,用于回显字符串

时间:2014-11-28 22:29:16

标签: php arrays

在这里遇到一堵墙,如果有人能帮忙解决这个问题,我会非常感激。

如果有人想知道我想要完成什么,我目前正在练习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分开并用for($a)echo后写for($b)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](结果现在更加混乱)

4 个答案:

答案 0 :(得分:1)

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

代码:

<?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";
  }

输出:

A cool car is the Ferrari F458 Super Car
A cool car is the Lamborghini Aventador Super Car
A cool car is the Aston Martin one-77 Super Car
A cool car is the Mercedes GT Super Car
A cool car is the Maserati GTS Super Car
A cool car is the McLaren MP4-12C Super Car

答案 1 :(得分:1)

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

答案 2 :(得分:0)

我会将此添加为评论,但我仍然没有50个代表......

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

编辑:你在任何地方都不使用“$ lengthTwo”。

答案 3 :(得分:0)

本着学习的精神:)另外四种方法来处理同一个问题:

每种方法

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开始,应该使用use operator,而不是global。至少可以说这是绝对的。

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