我有一个数组:
$list = array('string1', 'string2', 'string3');
我想要得到的指数给定值(即1
用于string2
与2
供string3
)
我想要的只是字符串在数组中的位置
- 字符串1为0
- string2是1
- string3是2
如何实现呢?
我有一个数组:
$list = array('string1', 'string2', 'string3');
我想要得到的指数给定值(即1
用于string2
与2
供string3
)
我想要的只是字符串在数组中的位置
如何实现呢?
Answers:
array_search
是这样做的方法。
array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : mixed
从文档:
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
您可以手动遍历数组并找到索引,但是为什么要在有此功能的情况下这样做呢?此函数始终返回键,并且将与关联数组和普通数组一起使用。
如果仅执行其中的一些操作(和/或数组大小很大),则使用array_search的方向正确:
$list = array('string1', 'string2', 'string3');
$k = array_search('string2', $list); //$k = 1;
如果您想要全部(或其中很多),则循环可能使您更好:
foreach ($list as $key => $value) {
echo $value . " in " . $key . ", ";
}
// Prints "string1 in 0, string2 in 1, string3 in 2, "
其他人提出了array_search()
可以提供找到值的数组元素键的建议。您可以使用以下命令确保数组键是连续的整数array_values()
:
$list = array(0=>'string1', 'foo'=>'string2', 42=>'string3');
$index = array_search('string2', array_values($list));
print "$index\n";
// result: 1
您在问题中说array_search()
没有用。你能解释为什么吗?您尝试了什么?它又如何满足您的需求?
//或考虑您的数组结构:
$array = array(
'string1' => array('a' => '', 'b' => '', 'c' => ''),
'string2' => array('a' => '', 'b' => '', 'c' => ''),
'string3' => array('a' => '', 'b' => '', 'c' => ''),
);
//您可以
function findIndexOfKey($key_to_index,$array){
return array_search($key_to_index,array_keys($array));
}
//执行
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";
//或者
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";
//递归地
print "\r\n//-- Method 3 --//\r\n";
foreach(array_keys($array) as $key => $value){
print '#index of: '.$value.' = '.$key."\r\n";
}
//输出
//-- 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
此代码应与新例程相同,并使用正确的多维数组。
$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));
您必须为此创建一个函数。我认为没有任何内置功能可用于此目的。默认情况下,所有PHP数组都是关联的。因此,如果不确定它们的密钥,请使用以下代码:
<?php
$given_array = array('Monday' => 'boring',
'Friday' => 'yay',
'boring',
'Sunday' => 'fun',
7 => 'boring',
'Saturday' => 'yay fun',
'Wednesday' => 'boring',
'my life' => 'boring');
$repeating_value = "boring";
function array_value_positions($array, $value){
$index = 0;
$value_array = array();
foreach($array as $v){
if($value == $v){
$value_array[$index] = $value;
}
$index++;
}
return $value_array;
}
$value_array = array_value_positions($given_array, $repeating_value);
$result = "The value '$value_array[0]' was found at these indices in the given array: ";
$key_string = implode(', ',array_keys($value_array));
echo $result . $key_string . "\n";//Output: The value 'boring' was found at these indices in the given array: 0, 2, 4, 6, 7
尝试使用array_keys PHP函数。
$key_string1 = array_keys($list, 'string1');
array_keys
和array_search
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);
或者对于索引,您可以使用
$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));
$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以将元素向上移动,将方向设置为1以将元素向下移动。例如:Move(['first'=>'Peter','second'=>'Paul','third'=>'Kate'],'second',-1)将使Paul向上移动,而Peter向下移动。
function Move($a,$element,$direction)
{
$temp = [];
$index = 0;
foreach($a as $key=>$value)
{
$temp[$index++] = $key;
}
$index = array_search($element,$temp);
$newpos = $index+$direction;
if(isset($temp[$newpos]))
{
$value2 = $temp[$newpos];
$temp[$newpos]=$element;
$temp[$index]=$value2;
}
else
{
# move is not possible
return $a; # returns the array unchanged
}
$final = [];
foreach($temp as $next)
{
$final[$next]=$a[$next];
}
return $final;
}