简体   繁体   English

php - 获取关联数组的数字索引

[英]php - get numeric index of associative array

I have an associative array and I need to find the numeric position of a key. 我有一个关联数组,我需要找到一个键的数字位置。 I could loop through the array manually to find it, but is there a better way build into PHP? 我可以手动遍历数组来找到它,但有没有更好的方法构建到PHP?

$a = array(
  'blue'   => 'nice',
  'car'    => 'fast',
  'number' => 'none'
);

// echo (find numeric index of $a['car']); // output: 1
echo array_search("car",array_keys($a));
$blue_keys = array_search("blue", array_keys($a));

http://php.net/manual/en/function.array-keys.php

function arrayValuePosition($value, $array)
{
    return array_search($value, array_keys($array));
}

  $a = array(
      'blue' => 'nice',
      'car' => 'fast',
      'number' => 'none'
  );  
var_dump(array_search('car', array_keys($a)));
var_dump(array_search('blue', array_keys($a)));
var_dump(array_search('number', array_keys($a)));

While Fosco's answer is not wrong there is a case to be considered with this one: mixed arrays. 虽然Fosco的答案没有错,但有一个案例需要考虑 :混合阵列。 Imagine I have an array like this: 想象一下,我有一个这样的数组:

$a = array(
  "nice",
  "car" => "fast",
  "none"
);

Now, PHP allows this kind of syntax but it has one problem: if I run Fosco's code I get 0 which is wrong for me, but why this happens? 现在,PHP允许这种语法,但它有一个问题:如果我运行Fosco的代码我得到0 对我来说是错误的,但为什么会发生这种情况?
Because when doing comparisons between strings and integers PHP converts strings to integers (and this is kinda stupid in my opinion), so when array_search() searches for the index it stops at the first one because apparently ("car" == 0) is true . 因为在进行字符串和整数之间的比较时, PHP会将字符串转换为整数 (在我看来这有点愚蠢),所以当array_search()搜索索引时,它会在第一个停止,因为显然("car" == 0) 是是的
Setting array_search() to strict mode won't solve the problem because then array_search("0", array_keys($a)) would return false even if an element with index 0 exists. array_search()设置为strict模式将无法解决问题,因为即使存在索引为0的元素, array_search("0", array_keys($a))也将返回false。
So my solution just converts all indexes from array_keys() to strings and then compares them correctly: 所以我的解决方案只是将array_keys()所有索引转换为字符串,然后正确地比较它们:

echo array_search("car", array_map("strval", array_keys($a)));

Prints 1 , which is correct. 打印1 ,这是正确的。

EDIT: 编辑:
As Shaun pointed out in the comment below, the same thing applies to the index value, if you happen to search for an int index like this: 正如Shaun在下面的评论中指出的那样,如果你碰巧搜索像这样的int索引,那么同样适用于索引值:

$a = array(
  "foo" => "bar",
  "nice",
  "car" => "fast",
  "none"
);
$ind = 0;
echo array_search($ind, array_map("strval", array_keys($a)));

You will always get 0 , which is wrong, so the solution would be to cast the index (if you use a variable) to a string like this: 你将永远得到0 ,这是错误的,所以解决方案是将索引(如果你使用一个变量)转换为这样的字符串:

$ind = 0;
echo array_search((string)$ind, array_map("strval", array_keys($a)));

a solution i came up with... probably pretty inefficient in comparison tho Fosco's solution: 我想出了一个解决方案......与Fosco的解决方案相比,效率可能相当低:

 protected function getFirstPosition(array$array, $content, $key = true) {

  $index = 0;
  if ($key) {
   foreach ($array as $key => $value) {
    if ($key == $content) {
     return $index;
    }
    $index++;
   }
  } else {
   foreach ($array as $key => $value) {
    if ($value == $content) {
     return $index;
    }
    $index++;
   }
  }
 }

All solutions based on array_keys don't work for mixed arrays. 所有基于array_keys的解决方案都不适用于混合数组。 Solution is simple: 解决方案很简单:

echo array_search($needle,array_keys($haystack), true);

From php.net: If the third parameter strict is set to TRUE then the array_search() function will search for identical elements in the haystack. 来自php.net:如果第三个参数strict设置为TRUE,那么array_search()函数将在haystack中搜索相同的元素。 This means it will also perform a strict type comparison of the needle in the haystack, and objects must be the same instance. 这意味着它还将对大海捞针进行严格的类型比较,并且对象必须是同一个实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM