PHP-使用ArraySearch按键反向搜索
我真的很喜欢摆弄数组。 从数组中,搜索是:PHP-使用ArraySearch按键反向搜索,php,arrays,Php,Arrays,我真的很喜欢摆弄数组。 从数组中,搜索是: Searches the array for a given value and returns the first corresponding key if successful 如果我有这样一个案例: $bill = [ '<label class="label label-default">Cleaning</label>' => 1, '<labe
Searches the array for a given value and returns the first corresponding key if successful
如果我有这样一个案例:
$bill = [
'<label class="label label-default">Cleaning</label>' => 1,
'<label class="label label-success">One Bar</label>' => 2,
'<label class="label label-warning">Test Periodic</label>' => 3,
'<label class="label label-primary">Repairs</label>' => 4,
];
return array_search($data['tagihan'], $bill);
$bill = [
1 => '<label class="label label-default">Cleaning</label>',
2 => '<label class="label label-success">One Bar</label>',
3 => '<label class="label label-warning">Test Periodic</label>',
4 => '<label class="label label-primary">Repairs</label>',
];
现在,如果我有一个这样的数组:
$bill = [
'<label class="label label-default">Cleaning</label>' => 1,
'<label class="label label-success">One Bar</label>' => 2,
'<label class="label label-warning">Test Periodic</label>' => 3,
'<label class="label label-primary">Repairs</label>' => 4,
];
return array_search($data['tagihan'], $bill);
$bill = [
1 => '<label class="label label-default">Cleaning</label>',
2 => '<label class="label label-success">One Bar</label>',
3 => '<label class="label label-warning">Test Periodic</label>',
4 => '<label class="label label-primary">Repairs</label>',
];
$bill=[
1=>“清洗”,
2=>1巴,
3=>“定期测试”,
4=>“修理”,
];
我怎样才能继续进行清洁
这是
$data['tagihan']=1
您可以使用搜索值轻松访问数组的键(如果存在):
echo $bill[$data['tagihan']];
function array_key_search($needle, $haystack) {
if(isset($haystack[$needle])) {
return $haystack[$needle];
} else {
return false;
}
}
或者一个函数,至少应该检查它是否存在:
echo $bill[$data['tagihan']];
function array_key_search($needle, $haystack) {
if(isset($haystack[$needle])) {
return $haystack[$needle];
} else {
return false;
}
}
看起来你错过了什么。我能够通过
$bill[$date['tagihan']]]
'$bill[$data[tagihan]]]'获得预期结果。。。或者我错过了什么?看起来你不需要函数。您可以只使用$bill[$data['tagihan']]
。(可能应该先检查是否设置了。)你放弃了吗???