我试图找出为什么我的代码没有返回与数组值相关联的键。该程序应该将项目的第一个字母与一个数组进行比较,其中字母A-M与" 1"的关键字相关联。和字母N-Z与" 2"的关键字相关联。表明他们将位于的过道。我得到的最常见错误是array_search的第二个参数不是数组,但我认为array_combine将两个数组合并为一个新数组,因此($ aisles,$ letters)应该产生1 => A ,1 => B等等。我在http://achamlin.gwiddle.co.uk/web182/Project3/HamlinProject3.php上可以找到使用此页面的网页表单。我一直得到相同的输出:" Banana位于Aisle。"没有列出实际的过道。谢谢你的帮助。
//create function to compare first letter of item to the aisle arrays
function checkAisle($term) {
$item= "banana";
$letters = array("A", "B", "C", "D", "E", "F", "G","H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T","U", "V", "W", "X", "Y", "Z");
$aisles = array("1", "1", "1", "1", "1", "1", "1","1", "1", "1", "1", "1", "1", "2", "2", "2", "2", "2", "2", "2","2", "2", "2", "2", "2", "2");$itemUC = UCfirst($item);
$guide = array_combine($aisles, $letters);
$itemUC = UCfirst($item);
$firstChar = $itemUC[0];
$location = array_search($firstChar, $guide);
echo "$itemUC is located on Aisle $location.";
}
//run function
checkAisle("banana");
答案 0 :(得分:3)
我要原谅你在你的功能中使用$term
但$item
这一事实(这可能是你尝试自我解决的结果,所以为了方便起见,我假设这是固定的... ...你的代码在array_combine()
行开始变坏了。如果你打电话:
var_export($guide);
你会看到:
array (
1 => 'M',
2 => 'Z',
)
这是因为您的键数组和值数组参数相反,结果是每个后续值和预先存在的键都会覆盖该值(键必须是唯一的)。然后,如果您尝试查找任何不在$guide
中作为值存在的字母,您将收到false
(布尔值,而不是字符串 - 它作为零长度字符串回显) array_search()
。因此,您的功能仅适用于以M
或Z
开头的字符串。
无论如何,这是错误的。如果你想改进你的代码......
您只需要隔离并大写第一个字母,然后将该字符与大写字母M
进行比较。
代码:(Demo)
function checkAisle($term) {
$location = ($itemUC = strtoupper($term[0])) < "M" ? 1 : 2;
return "$itemUC is located on Aisle $location.";
}
echo checkAisle("banana") , "\n";
echo checkAisle("guava") , "\n";
echo checkAisle("lemon") , "\n";
echo checkAisle("melon") , "\n";
echo checkAisle("nectarine") , "\n";
echo checkAisle("Trump");
输出:
B is located on Aisle 1.
G is located on Aisle 1.
L is located on Aisle 1.
M is located on Aisle 2.
N is located on Aisle 2.
T is located on Aisle 2.
这是另一个版本,它包含一个生成的查找数组,并检查第一个字符实际上是一个字母:
代码:(Demo)
function checkAisle($term) {
if (!ctype_alpha($term[0])) {
return "First character out of bounds";
}
$locations = array_fill_keys(range('A','L'),1)+array_fill_keys(range('M','Z'),2);
$letter = strtoupper($term[0]);
return "$letter is located on Aisle {$locations[$letter]}.";
}
echo checkAisle("Trump") , "\n";
echo checkAisle("banana") , "\n";
echo checkAisle("melon") , "\n";
echo checkAisle("guava") , "\n";
echo checkAisle("nectarine") , "\n";
echo checkAisle("lemon") , "\n";
答案 1 :(得分:1)
因为您将$aisles
数组指定为组合数组键并将$letters
指定为值,而您无法将其指定为切换它,然后只需按键/字母选择数组即可返回过道。
<?php
//create function to compare first letter of item to the aisle arrays
function checkAisle($item) {
$letters = range('A', 'Z');
$aisles = array("1", "1", "1", "1", "1", "1", "1","1", "1", "1", "1", "1", "1", "2", "2", "2", "2", "2", "2", "2","2", "2", "2", "2", "2", "2");
$guide = array_combine($letters, $aisles);
$itemUC = ucfirst($item);
$firstChar = $itemUC[0];
echo "$itemUC is located on Aisle $guide[$firstChar].";
}
//run function
checkAisle("banana");
<强>结果:强>
Banana is located on Aisle 1.
或者更好的方式(处理错误),假设您可能想要为硬编码阵列添加更多通道,或者字母A实际上位于过道2
而不是1
。
<?php
//create function to compare first letter of item to the aisle arrays
function checkAisle($item = null) {
// check param
if (empty($item) || is_numeric($item)) {
throw new InvalidArgumentException('Error: function checkAisle is expecting string');
}
// build data
$letters = range('A', 'Z');
$aisles = [
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2"
];
$guide = array_combine($letters, $aisles);
//
$itemUC = ucfirst($item);
$firstChar = isset($itemUC[0]) ? $itemUC[0] : null;
if (!isset($guide[$firstChar])) {
return 'Item '.$itemUC.' not found in store.';
}
return $itemUC.' is located on Aisle '.$guide[$firstChar];
}
//run function
try {
echo checkAisle("banana").PHP_EOL;
echo checkAisle("twix").PHP_EOL;
echo checkAisle("-").PHP_EOL;
echo checkAisle(123).PHP_EOL;
} catch (InvalidArgumentException $e) {
echo $e->getMessage();
}
答案 2 :(得分:0)
<?php
//create function to compare first letter of item to the aisle arrays
function checkAisle($term) {
$item= "banana";
$letters = array("A", "B", "C", "D", "E", "F", "G","H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T","U", "V", "W", "X", "Y", "Z");
$aisles = array("1", "1", "1", "1", "1", "1", "1","1", "1", "1", "1", "1", "1", "2", "2", "2", "2", "2", "2", "2","2", "2", "2", "2", "2", "2");
$itemUC = UCfirst($item);
$guide = array_combine($letters, $aisles);
$itemUC = UCfirst($item);
$firstChar = $itemUC[0];
$location = $guide[$firstChar];
echo "$itemUC is located on Aisle $location.";
}
//run function
checkAisle("banana");
使用array_combine,你重复使用密钥1,它会覆盖所有其他密钥。
答案 3 :(得分:0)
嗯,这是我的解决方案。当没有找到有效的字母时,它将返回0。您可以轻松添加多个过道。
function checkAisle($name) {
$letter = strtolower($name[0]);
$result = 0;
if(in_array($letter, range('a', 'm'))){
$result = 1;
}
if(in_array($letter, range('n', 'z'))){
$result = 2;
}
// aisle 3, 4, 5, ... here
return $result;
}