我有以下代码
public static $_myarray = array('This is S' => 'S',
'This is RC' => 'RC',
'This is RF' => 'RF',
'This is C' => 'C');
$check_var = 'S';
我想要得到这个
echo $key_of_array = array_search($check_var,$_myarray); // output should be 'This is S'
它返回空白值而不是“This is S”
我也试过以下。
- >类型转换为$ check_var
- > array_search($ check_var,$ _ myArray的,真正的);
但没有运气。有人可以帮我解决这个问题吗?
注意:它适用于其他值..问题只与数组的第一个元素..我在Linux上检查这个
答案 0 :(得分:0)
直接来自docs,Returns the key for needle if it is found in the array, FALSE otherwise.
所以这里是空字符串的来源。
$_myarray = [
'This is S' => 'S',
'This is RC' => 'RC',
'This is RF' => 'RF',
'This is C' => 'C'
];
$check_var = 'S';
echo array_search($check_var,$_myarray);
echo array_search('S',$_myarray);
适合我。
答案 1 :(得分:0)
您的代码有效,但如果您不在课程范围内,则应该给出错误..
检查此fiddle
<?php
//echo phpinfo();
/*public*/ $_myarray = array('This is S' => 'S', //note that public keyword causes an error out of class scope..
'This is RC' => 'RC',
'This is RF' => 'RF',
'This is C' => 'C');
$check_var = 'S';
echo $key_of_array = array_search($check_var,$_myarray); // output should be 'This is S'