I have an array:
我有一个数组:
$list = array('string1', 'string2', 'string3');
I want to get the index for a given value (i.e. 1
for string2
and 2
for string3
)
我想获取给定值的索引(例如,string2的索引和string3的索引)
All I want is the position of the strings in the array
我想要的是数组中字符串的位置。
- string1 is 0
- string1是0
- string2 is 1
- string2相等是1
- string3 is 2
- string3是2
How to achieve this?
如何实现呢?
10 个解决方案
#1
90
array_search
is the way to do it.
array_search就是这样做的。
From the docs:
从文档:
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
You could loop over the array manually and find the index but why do it when there's a function for that. This function always returns a key and it will work well with associative and normal arrays.
您可以手动循环数组并找到索引,但是为什么要这样做呢?这个函数总是返回一个键,它可以很好地处理关联数组和普通数组。
#2
14
If you're only doing a few of them (and/or the array size is large), then you were on the right track with array_search:
如果您只做了其中的几个(并且/或数组大小较大),那么您使用array_search的方向是正确的:
$list = array('string1', 'string2', 'string3');
$k = array_search('string2', $list); //$k = 1;
If you want all (or a lot of them), a loop will prob do you better:
如果你想要所有的(或很多),循环会让你做得更好:
foreach ($list as $key => $value) {
echo $value . " in " . $key . ", ";
}
// Prints "string1 in 0, string2 in 1, string3 in 2, "
#3
7
Other folks have suggested array_search()
which gives the key of the array element where the value is found. You can ensure that the array keys are contiguous integers by using array_values()
:
另一些人建议使用array_search(),它提供查找值的数组元素的键。可以使用array_values()确保数组键是连续整数:
$list = array(0=>'string1', 'foo'=>'string2', 42=>'string3');
$index = array_search('string2', array_values($list));
print "$index\n";
// result: 1
You said in your question that array_search()
was no use. Can you explain why? What did you try and how did it not meet your needs?
您在您的问题中说array_search()没有用。你能解释一下为什么?你试过什么,它怎么不满足你的需要?
#4
4
// or considering your array structure:
//或考虑数组结构:
$array = array(
'string1' => array('a' => '', 'b' => '', 'c' => ''),
'string2' => array('a' => '', 'b' => '', 'c' => ''),
'string3' => array('a' => '', 'b' => '', 'c' => ''),
);
// you could just
/ /你可以
function findIndexOfKey($key_to_index,$array){
return array_search($key_to_index,array_keys($array));
}
// executed
/ /执行
print "\r\n//-- Method 1 --//\r\n";
print '#index of: string1 = '.findIndexofKey('string1',$array)."\r\n";
print '#index of: string2 = '.findIndexofKey('string2',$array)."\r\n";
print '#index of: string3 = '.findIndexofKey('string3',$array)."\r\n";
// alternatively
/ /或者
print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search('string1',array_keys($array))."\r\n";
print '#index of: string2 = '.array_search('string2',array_keys($array))."\r\n";
print '#index of: string3 = '.array_search('string3',array_keys($array))."\r\n";
// recursersively
/ / recursersively
print "\r\n//-- Method 3 --//\r\n";
foreach(array_keys($array) as $key => $value){
print '#index of: '.$value.' = '.$key."\r\n";
}
// outputs
/ /输出
//-- Method 1 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 2 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 3 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
#5
4
The problem is that you don't have a numerical index on your array.
Using array_values() will create a zero indexed array that you can then search using array_search() bypassing the need to use a for loop.
问题是数组中没有数值索引。使用array_values()将创建一个零索引数组,然后您可以使用array_search()搜索该数组,而无需使用for循环。
$list = array('string1', 'string2', 'string3');
$index = array_search('string2',array_values($list));
#6
1
Try the array_keys PHP function.
试试array_keys PHP函数。
$key_string1 = array_keys($list, 'string1');
#7
1
Could you be a little more specific?
你能说得更具体一点吗?
$key = array_search('string2',$list)
works fine for me. Are you trying to accomplish something more complex?
对我来说很不错。你想要完成更复杂的事情吗?
#8
1
This code should do the same as your new routine, working with the correct multi-dimensional array..
这段代码应该与您的新例程一样,使用正确的多维数组。
$arr = array(
'string1' => array('a' => '', 'b' => '', 'c' => ''),
'string2' => array('a' => '', 'b' => '', 'c' => ''),
'string3' => array('a' => '', 'b' => '', 'c' => '')
);
echo 'Index of "string2" = '. array_search('string2', array_keys($arr));
#9
0
array_search should work fine, just tested this and it returns the keys as expected:
array_search应该工作得很好,只是测试了一下,它会按预期返回键:
$list = array('string1', 'string2', 'string3');
echo "Key = ".array_search('string1', $list);
echo " Key = ".array_search('string2', $list);
echo " Key = ".array_search('string3', $list);
Or for the index, you could use
或者对于索引,你可以用
$list = array('string1', 'string2', 'string3');
echo "Index = ".array_search('string1', array_merge($list));
echo " Index = ".array_search('string2', array_merge($list));
echo " Index = ".array_search('string3', array_merge($list));
#10
0
$find="Topsite";
$list=array("Tope","Ajayi","Topsite","Infotech");
$list_count=count($list);
sort($list);
for($i=0;$i<$list_count;$i++)
{
if($list[$i]==$find){
$position=$i;
}
}
echo $position;
#1
90
array_search
is the way to do it.
array_search就是这样做的。
From the docs:
从文档:
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
You could loop over the array manually and find the index but why do it when there's a function for that. This function always returns a key and it will work well with associative and normal arrays.
您可以手动循环数组并找到索引,但是为什么要这样做呢?这个函数总是返回一个键,它可以很好地处理关联数组和普通数组。
#2
14
If you're only doing a few of them (and/or the array size is large), then you were on the right track with array_search:
如果您只做了其中的几个(并且/或数组大小较大),那么您使用array_search的方向是正确的:
$list = array('string1', 'string2', 'string3');
$k = array_search('string2', $list); //$k = 1;
If you want all (or a lot of them), a loop will prob do you better:
如果你想要所有的(或很多),循环会让你做得更好:
foreach ($list as $key => $value) {
echo $value . " in " . $key . ", ";
}
// Prints "string1 in 0, string2 in 1, string3 in 2, "
#3
7
Other folks have suggested array_search()
which gives the key of the array element where the value is found. You can ensure that the array keys are contiguous integers by using array_values()
:
另一些人建议使用array_search(),它提供查找值的数组元素的键。可以使用array_values()确保数组键是连续整数:
$list = array(0=>'string1', 'foo'=>'string2', 42=>'string3');
$index = array_search('string2', array_values($list));
print "$index\n";
// result: 1
You said in your question that array_search()
was no use. Can you explain why? What did you try and how did it not meet your needs?
您在您的问题中说array_search()没有用。你能解释一下为什么?你试过什么,它怎么不满足你的需要?
#4
4
// or considering your array structure:
//或考虑数组结构:
$array = array(
'string1' => array('a' => '', 'b' => '', 'c' => ''),
'string2' => array('a' => '', 'b' => '', 'c' => ''),
'string3' => array('a' => '', 'b' => '', 'c' => ''),
);
// you could just
/ /你可以
function findIndexOfKey($key_to_index,$array){
return array_search($key_to_index,array_keys($array));
}
// executed
/ /执行
print "\r\n//-- Method 1 --//\r\n";
print '#index of: string1 = '.findIndexofKey('string1',$array)."\r\n";
print '#index of: string2 = '.findIndexofKey('string2',$array)."\r\n";
print '#index of: string3 = '.findIndexofKey('string3',$array)."\r\n";
// alternatively
/ /或者
print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search('string1',array_keys($array))."\r\n";
print '#index of: string2 = '.array_search('string2',array_keys($array))."\r\n";
print '#index of: string3 = '.array_search('string3',array_keys($array))."\r\n";
// recursersively
/ / recursersively
print "\r\n//-- Method 3 --//\r\n";
foreach(array_keys($array) as $key => $value){
print '#index of: '.$value.' = '.$key."\r\n";
}
// outputs
/ /输出
//-- Method 1 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 2 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 3 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
#5
4
The problem is that you don't have a numerical index on your array.
Using array_values() will create a zero indexed array that you can then search using array_search() bypassing the need to use a for loop.
问题是数组中没有数值索引。使用array_values()将创建一个零索引数组,然后您可以使用array_search()搜索该数组,而无需使用for循环。
$list = array('string1', 'string2', 'string3');
$index = array_search('string2',array_values($list));
#6
1
Try the array_keys PHP function.
试试array_keys PHP函数。
$key_string1 = array_keys($list, 'string1');
#7
1
Could you be a little more specific?
你能说得更具体一点吗?
$key = array_search('string2',$list)
works fine for me. Are you trying to accomplish something more complex?
对我来说很不错。你想要完成更复杂的事情吗?
#8
1
This code should do the same as your new routine, working with the correct multi-dimensional array..
这段代码应该与您的新例程一样,使用正确的多维数组。
$arr = array(
'string1' => array('a' => '', 'b' => '', 'c' => ''),
'string2' => array('a' => '', 'b' => '', 'c' => ''),
'string3' => array('a' => '', 'b' => '', 'c' => '')
);
echo 'Index of "string2" = '. array_search('string2', array_keys($arr));
#9
0
array_search should work fine, just tested this and it returns the keys as expected:
array_search应该工作得很好,只是测试了一下,它会按预期返回键:
$list = array('string1', 'string2', 'string3');
echo "Key = ".array_search('string1', $list);
echo " Key = ".array_search('string2', $list);
echo " Key = ".array_search('string3', $list);
Or for the index, you could use
或者对于索引,你可以用
$list = array('string1', 'string2', 'string3');
echo "Index = ".array_search('string1', array_merge($list));
echo " Index = ".array_search('string2', array_merge($list));
echo " Index = ".array_search('string3', array_merge($list));
#10
0
$find="Topsite";
$list=array("Tope","Ajayi","Topsite","Infotech");
$list_count=count($list);
sort($list);
for($i=0;$i<$list_count;$i++)
{
if($list[$i]==$find){
$position=$i;
}
}
echo $position;