我在PHP中有这个数组:
array(
[0] => array( 'username' => 'user1' )
[1] => array( 'username' => 'user2' )
)
如果我有'username'字符串,我怎样才能将索引值作为数字?
例如,如果我有'user1',我怎么能得到0?
答案 0 :(得分:28)
看看array_search。
从PHP帮助文件:
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
?>
答案 1 :(得分:25)
如果您有一个2D数组,就像在您的示例中一样,您需要稍微自定义一些内容:
function array_search2d($needle, $haystack) {
for ($i = 0, $l = count($haystack); $i < $l; ++$i) {
if (in_array($needle, $haystack[$i])) return $i;
}
return false;
}
$myArray = array(
array( 'username' => 'user1' ),
array( 'username' => 'user2' )
);
$searchTerm = "user1";
if (false !== ($pos = array_search2d($searchTerm, $myArray))) {
echo $searchTerm . " found at index " . $pos;
} else {
echo "Could not find " . $searchTerm;
}
如果您只想在一个特定字段中搜索,可以将功能更改为以下内容:
function array_search2d_by_field($needle, $haystack, $field) {
foreach ($haystack as $index => $innerArray) {
if (isset($innerArray[$field]) && $innerArray[$field] === $needle) {
return $index;
}
}
return false;
}
答案 2 :(得分:6)
这很简单
private function getArrayKey($haystack, $needle)
{
foreach($haystack as $key => $product)
{
if ($product['id'] === $needle)
return $key;
}
return false;
}
答案 3 :(得分:3)
如果您知道密钥为username
,只需使用数组作为搜索参数:
$username = 'user1';
$key = array_search(array('username' => $username), $array);
答案 4 :(得分:2)
也许一起使用array_filter
和array_keys
会有所帮助。
基于类的方法。
<?php
class ArraySearch2d {
static protected $_key;
static protected $_value;
static function isMatch($element)
{
if (!is_array($element)) return false;
return $element[self::$_key] == self::$_value;
}
static function filter(array $arrayToSearch, $key, $value)
{
if (!is_string($key)) throw new Exception("Array Key must be a string");
self::$_key = $key;
self::$_value = $value;
return array_filter($arrayToSearch, 'ArraySearch2d::isMatch');
}
// to directly answer your question.
static function getIndex(array $arrayToSearch, $key, $value)
{
$matches = self::filter($arrayToSearch, $key, $value);
if (!count($matches)) return false;
$indexes = array_keys($matches);
return $indexes[0];
}
}
$array = array("1"=>array('username'=>'user1'), "3"=>array('username'=>'user2'));
$matches = ArraySearch2d::filter($array, 'username', 'user2');
var_dump($matches);
$indexs = array_keys($matches);
var_dump($indexs);
// Demonstrating quick answer:
echo "Key for first 'username'=>'user1' element is: "
.ArraySearch2d::getIndex($array, 'username', 'user1')."\n";
产地:
array(1) {
[3]=>
array(1) {
["username"]=>
string(5) "user2"
}
}
array(1) {
[0]=>
int(3)
}
Key for first 'username'=>'user1' element is: 1
不使用类 - 这会产生相同的结果:
<?php
$field="username";
$value = "user2";
function usernameMatch($element)
{
global $field, $value;
if (!is_array($element)) return false;
return $element[$field] == $value;
}
function getFirstIndex(array $array)
{
if (!count($array)) return false;
$indexes = array_keys($array);
return $indexes[0];
}
$array = array("1"=>array('username'=>'user1'), "3"=>array('username'=>'user2'));
$matches = array_filter($array, 'usernameMatch');
var_dump($matches);
$indexs = array_keys($matches);
var_dump($indexs);
// Demonstrating quick answer - and why you should probably use the class-
// you don't want to have to remember these "globals" all the time.
$field = 'username';
$value = 'user1';
echo "Key for first 'username'=>'user1' element is: "
.getFirstIndex(array_filter($array, 'usernameMatch'));