我在一个函数中有一个for循环办公区域(101,102),它应循环遍历数组以获取匹配的OfficeRegion的OLevel(红色,琥珀色,黄色)中的值并返回一个值
我遇到的问题是,只有数组中的第一行返回一个值并且它总是返回"#red#red",我怎么能阻止它返回2个值并让它循环通过整个阵列?
数组:
array(115) {
[0]=> object(stdClass)#93 (2) { ["OfficeRegion"]=> string(3) "101" ["OLevel"]=> string(1) "R" }
[1]=> object(stdClass)#94 (2) { ["OfficeRegion"]=> string(3) "102" ["OLevel"]=> string(1) "R" }
[2]=> object(stdClass)#95 (2) { ["OfficeRegion"]=> string(3) "103" ["OLevel"]=> string(1) "R" }
[3]=> object(stdClass)#96 (2) { ["OfficeRegion"]=> string(3) "201" ["OLevel"]=> string(1) "R" }
[4]=> object(stdClass)#97 (2) { ["OfficeRegion"]=> string(3) "202" ["OLevel"]=> string(1) "R" }
[5]=> object(stdClass)#98 (2) { ["OfficeRegion"]=> string(3) "301" ["OLevel"]=> string(1) "R" }
[6]=> object(stdClass)#99 (2) { ["OfficeRegion"]=> string(3) "302" ["OLevel"]=> string(1) "R" }
[7]=> object(stdClass)#100 (2) { ["OfficeRegion"]=> string(3) "401" ["OLevel"]=> string(1) "R" }
[8]=> object(stdClass)#101 (2) { ["OfficeRegion"]=> string(3) "403" ["OLevel"]=> string(1) "R" }
}
每个人:
foreach($sqlarray as $index => $columns) {
foreach($columns as $key => $value) {
if ($key == 'OfficeRegion' && $value == $OfficeRegion) {
if($columns->OLevel == "R") {
return '#red';
} elseif ($columns->OLevel == "O") {
return '#amber';
} elseif ($columns->OLevel == "Y") {
return '#yellow';
} else {
return '#green';
}
} elseif ($key == 'OfficeRegion' && $value != $OfficeRegion) {
return "#green";
}
}
}
欢迎任何建议!感谢
答案 0 :(得分:1)
如果$OfficeRegion
是数组,你应该使用这样的代码,你不需要内部循环:
foreach($sqlarray as $index => $columns) {
if (in_array($columns->OfficeRegion, $OfficeRegion)) {
switch ($columns->OLevel) {
case 'R':
echo '#red';
case 'O':
echo '#amber';
case 'Y':
echo '#yellow';
default:
echo '#green';
}
} else {
echo '#green';
}
}
我也在这里return
更改为echo
以循环播放您可能想要的所有项目。