尝试从多维数组中获取Key的值

时间:2021-10-19 13:35:33

I have an array thus (got this using var_dump):

我有一个数组(使用var_dump得到这个):

array
  0 => 
    array
      'post_id' => string '6' (length=1)
  1 => 
    array
      'post_id' => string '9' (length=1)

I want to get the key when I have just the post_id. For example, I want '0' returned if I have '6' and '1' if I have '9'. I have tried:

我想拥有post_id时获取密钥。例如,如果我有'6',我想要'0'返回,如果我有'9',我想要'1'。我试过了:

$key = array_keys($subs, array_keys($subs[??], 6));

given that the $subs is the array. The issue is, I don't know how to iterate through the array 'within' the 'parent' array, hence, the '??'

假设$ subs是数组。问题是,我不知道如何遍历“父”数组内的数组,因此,'??'

2 个解决方案

#1


1  

I would use array_filter() and use to do this. Like this:

我会使用array_filter()并使用它来做到这一点。像这样:

$array; // your array
$needle; // the value you are looking for
$filtered_array = array_filter($array, function ($element) use ($needle) {
    return ($element['post_id'] === $needle);
});

$matching_keys = array_keys($filtered_array);

The array_filter() will filter down the input array to only those array element arrays that have a value for post_id that matches the value of $needle. You can use array_keys to get the key values for the remaining element(s) after the filter has been applied.

array_filter()将向下过滤输入数组,只将那些具有post_id值的数组元素数组与$ needle的值相匹配。在应用过滤器后,您可以使用array_keys获取剩余元素的键值。

#2


1  

$post_id_to_find = '6';
$key = '';
foreach ($subs as $k1 => $v1)
{
    foreach ($v1 as $k2 => $v2)
    {
        if ($post_id_to_find == $v2)
        {
            $key = $k1;
            break;
        }
    }
}

Essentially what this code does is loop through the outer array and for each element loop through inner array, and if it finds the post id you want it'll set a variable that was initalized outside of the loops so after you break out of the loops you'll have the appropriate key.

基本上这个代码所做的是循环遍历外部数组,并为每个元素循环通过内部数组,如果它找到你想要的post id,它将设置一个在循环外部初始化的变量,所以在你打破循环之后你会有合适的钥匙。


EDIT

This is actually a pretty crappy answer, I realized you don't really need the inner loop since you know what key you want to check in the inner arrays... Anyway it seems like you used another answer, so this is pretty much moot.

这实际上是一个非常糟糕的答案,我意识到你并不真的需要内循环,因为你知道要在内部数组中检查什么键...无论如何,你似乎使用了另一个答案,所以这几乎没有实际意义。

#1


1  

I would use array_filter() and use to do this. Like this:

我会使用array_filter()并使用它来做到这一点。像这样:

$array; // your array
$needle; // the value you are looking for
$filtered_array = array_filter($array, function ($element) use ($needle) {
    return ($element['post_id'] === $needle);
});

$matching_keys = array_keys($filtered_array);

The array_filter() will filter down the input array to only those array element arrays that have a value for post_id that matches the value of $needle. You can use array_keys to get the key values for the remaining element(s) after the filter has been applied.

array_filter()将向下过滤输入数组,只将那些具有post_id值的数组元素数组与$ needle的值相匹配。在应用过滤器后,您可以使用array_keys获取剩余元素的键值。

#2


1  

$post_id_to_find = '6';
$key = '';
foreach ($subs as $k1 => $v1)
{
    foreach ($v1 as $k2 => $v2)
    {
        if ($post_id_to_find == $v2)
        {
            $key = $k1;
            break;
        }
    }
}

Essentially what this code does is loop through the outer array and for each element loop through inner array, and if it finds the post id you want it'll set a variable that was initalized outside of the loops so after you break out of the loops you'll have the appropriate key.

基本上这个代码所做的是循环遍历外部数组,并为每个元素循环通过内部数组,如果它找到你想要的post id,它将设置一个在循环外部初始化的变量,所以在你打破循环之后你会有合适的钥匙。


EDIT

This is actually a pretty crappy answer, I realized you don't really need the inner loop since you know what key you want to check in the inner arrays... Anyway it seems like you used another answer, so this is pretty much moot.

这实际上是一个非常糟糕的答案,我意识到你并不真的需要内循环,因为你知道要在内部数组中检查什么键...无论如何,你似乎使用了另一个答案,所以这几乎没有实际意义。