我有简单的嵌套foreach()
,但我没有得到我想的那样。
这是完整的代码:
public function namelist() {
return array('lastname', 'firstname', 'middlename');
}
public function nameRequest($namelist) { // either lastname, firstname or middlename
switch($namelist) {
case 'lastname':
$request = array('ramirez', 'corazon', 'gonzaga');
break;
case 'firstname':
$request = array('michael', 'michaela', 'angel');
break;
case 'middlename':
$request = array('osaka', 'oscar', 'roxas');
break;
}
return $request;
}
我想要一个这样的结果:
Lastname: ramirez, corazon, gonzaga
Firstname: michael, michaela, angel
Middlename: osaka, oscar, roxas
但是当我尝试像这样编码时:
public function fullname() {
$namelist = '';
$namedata = '';
$namelists = self::namelist();
foreach ($namelists as $names) {
$namelist .= $names . ': ';
$namerequest = self::nameRequest($names);
foreach ($namerequest as $namerequests){
$namedata .= $namerequests . ',';
}
}
return $namelist . ': ' . $namedata;
}
它返回:
lastname: firstname: middlename: ramirez, corazon, gonzaga, michael, michaela, angel, osaka, oscar, roxas
那我怎么解决这个问题呢?
答案 0 :(得分:1)
如果更新循环,这会更有意义:
public function fullname() {
$namelist = '';
$namedata = '';
$namelists = self::namelist();
$temp = '';
foreach ($namelists as $names) {
$namerequest = self::nameRequest($names);
foreach ($namerequest as $namerequests){
$namedata .= $namerequests . ',';
}
$temp .= $names . ': ' . $namedata.'<br/>';
}
return $temp;
}
答案 1 :(得分:1)
有一些错位的陈述:
<pre>
<?php
function namelist() {
return array('lastname', 'firstname', 'middlename');
}
function nameRequest($namelist) { // either lastname, firstname or middlename
switch($namelist) {
case 'lastname':
$request = array('ramirez', 'corazon', 'gonzaga');
break;
case 'firstname':
$request = array('michael', 'michaela', 'angel');
break;
case 'middlename':
$request = array('osaka', 'oscar', 'roxas');
break;
}
return $request;
}
function fullname() {
$namelist = '';
$namelists = namelist();
foreach ($namelists as $names) {
$namedata = ''; // moved
$namerequest = nameRequest($names);
foreach ($namerequest as $namerequests){
$namedata .= $namerequests . ',';
}
$namelist .= $names . ': ' . $namedata . "\n"; // new
}
return $namelist; // modified
}
echo fullname();
?>
注意:我删除了测试的面向对象 测试:http://phpfiddle.org/main/code/v82x-czc4
另请考虑使用implode()
代替内部foreach
。它更短,将解决您仍然存在的尾随,
问题。
答案 2 :(得分:1)
使用此代码,从上面的代码开始,它非常简单,简单和简短。因为foreach循环在上面的代码上运行了2次,所以它会影响执行时间,所以请试试这个
<?php
public function namelist() {
return array('lastname', 'firstname', 'middlename');
}
public function nameRequest($namelist) { // either lastname, firstname or middlename
switch($namelist) {
case 'lastname':
$request = array('ramirez', 'corazon', 'gonzaga');
break;
case 'firstname':
$request = array('michael', 'michaela', 'angel');
break;
case 'middlename':
$request = array('osaka', 'oscar', 'roxas');
break;
}
return $request;
}
public function fullname() {
$namelists = self::namelist();
$fullname = '';
foreach ($namelists as $names) {
$namerequest = self::nameRequest($names);
$nameIs = implode(",", $namerequest);
$fullname .= $names.": " . $nameIs . "<br>";
}
return $fullname;
}
echo fullname();
?>