php - 查找键具有给定值的数组

时间:2022-03-24 21:27:08

I have an array of dictionnaries like:

我有一系列的词典,如:

$arr = array(
    array(
        'id' => '1',
        'name' => 'machin',
    ),
    array(
        'id' => '2',
        'name' => 'chouette',
    ),
);

How can I find the name of the array containing the id 2 (chouette) ?

如何找到包含id 2(chouette)的数组的名称?

Am I forced to reindex the array ?

我*重新索引数组吗?


Thank you all, aparently I'm forced to loop through the array (what I wanted to avoid), I thought that it were some lookup fonctions like Python. So I think I'll reindex with id.

谢谢大家,显然我*循环遍历数组(我想避免的),我认为这是一些像Python这样的查找功能。所以我想我会用id重新索引。

6 个解决方案

#1


3  

Just find the index of array that contains the id you want to find. SO has enough questions and answers on this topic available.

只需找到包含您要查找的ID的数组的索引。 SO有足够的问题和答案可供选择。

Assuming you have a big array with lots of data in your real application, it might be too slow (for your taste). In this case, you indeed need to modify the structure of your arrays, so you can look it up faster, e.g. by using the id as an index for the name (if you are only interested in the name).

假设你的实际应用程序中有一个包含大量数据的大数组,它可能太慢了(根据你的喜好)。在这种情况下,您确实需要修改数组的结构,以便您可以更快地查找它,例如通过使用id作为名称的索引(如果您只对名称感兴趣)。

#2


2  

As a for loop would be the best way to do this, I would suggest changing you array so that the id is the arrays index. For example:

因为for循环是最好的方法,我建议更改你的数组,以便id是数组索引。例如:

$arr = array(
    1 => 'machin',
    2 => 'chouette',
);

This way you could just get the name for calling $arr[2]. No looping and keeping your program running in linear time.

这样你就可以得到调用$ arr [2]的名字。没有循环并保持程序在线性时间内运行。

#3


1  

$name;
foreach ($arr as $value){
    if ( $value['id'] == 2  ){
        $name = $value['name'];
        break;
    }
}

#4


1  

I would say that it might be very helpful to reindex the information. If the ID is unique try something like this:

我想说重新索引信息可能会非常有帮助。如果ID是唯一的,请尝试以下方法:

$newarr = array();
for($i = 0;$i < count($arr);$i++){ $newarr[$arr[$i]['id']] = $arr[$i]['name']; }

The result would be:

结果将是:

$newarr = array('1'=>'machin','2'=>'chouette');

Then you can go trough the array with "foreach" like this:

然后你可以用这样的“foreach”来通过数组:

foreach($newarr as $key => $value){
    if($value == "machin"){
        return $key;
    }
}

But of course the same would work with your old array:

但是当然对你的旧阵列也一样:

foreach($arr as $item){
    if($item['name'] == "machin"){
        return $item['id'];
    }
}

It depends on what you are planning to do with the array ;-)

这取决于你打算用数组做什么;-)

#5


0  

array_key_exist() is the function to check for keys. foreach will help you get down in the multidimensional array. This function will help you get the name element of an array and let you specify a different id value.

array_key_exist()是检查密钥的函数。 foreach将帮助您进入多维数组。此函数将帮助您获取数组的name元素,并允许您指定不同的id值。

function findKey($bigArray, $idxVal) {
    foreach($bigArray as $array) {
        if(array_key_exists('id', $array) && $array['id'] == $idxVal) {
            return $array['name'];
        }
    }
    return false;
}

//Supply your array for $arr
print(findKey($arr, '2')); //"chouette"

#6


0  

It's a bit crude, but this would get you the name...

它有点粗糙,但这会让你得到这个名字......

$name = false;
foreach($arr as $v) {
    if($v['id'] == '2') {
         $name = $v['name'];
         break;
    }   
}
echo $name;

So no, you are not forced to reindex the array, but it would make things easier.

所以不,你没有*重新索引数组,但它会让事情变得更容易。

#1


3  

Just find the index of array that contains the id you want to find. SO has enough questions and answers on this topic available.

只需找到包含您要查找的ID的数组的索引。 SO有足够的问题和答案可供选择。

Assuming you have a big array with lots of data in your real application, it might be too slow (for your taste). In this case, you indeed need to modify the structure of your arrays, so you can look it up faster, e.g. by using the id as an index for the name (if you are only interested in the name).

假设你的实际应用程序中有一个包含大量数据的大数组,它可能太慢了(根据你的喜好)。在这种情况下,您确实需要修改数组的结构,以便您可以更快地查找它,例如通过使用id作为名称的索引(如果您只对名称感兴趣)。

#2


2  

As a for loop would be the best way to do this, I would suggest changing you array so that the id is the arrays index. For example:

因为for循环是最好的方法,我建议更改你的数组,以便id是数组索引。例如:

$arr = array(
    1 => 'machin',
    2 => 'chouette',
);

This way you could just get the name for calling $arr[2]. No looping and keeping your program running in linear time.

这样你就可以得到调用$ arr [2]的名字。没有循环并保持程序在线性时间内运行。

#3


1  

$name;
foreach ($arr as $value){
    if ( $value['id'] == 2  ){
        $name = $value['name'];
        break;
    }
}

#4


1  

I would say that it might be very helpful to reindex the information. If the ID is unique try something like this:

我想说重新索引信息可能会非常有帮助。如果ID是唯一的,请尝试以下方法:

$newarr = array();
for($i = 0;$i < count($arr);$i++){ $newarr[$arr[$i]['id']] = $arr[$i]['name']; }

The result would be:

结果将是:

$newarr = array('1'=>'machin','2'=>'chouette');

Then you can go trough the array with "foreach" like this:

然后你可以用这样的“foreach”来通过数组:

foreach($newarr as $key => $value){
    if($value == "machin"){
        return $key;
    }
}

But of course the same would work with your old array:

但是当然对你的旧阵列也一样:

foreach($arr as $item){
    if($item['name'] == "machin"){
        return $item['id'];
    }
}

It depends on what you are planning to do with the array ;-)

这取决于你打算用数组做什么;-)

#5


0  

array_key_exist() is the function to check for keys. foreach will help you get down in the multidimensional array. This function will help you get the name element of an array and let you specify a different id value.

array_key_exist()是检查密钥的函数。 foreach将帮助您进入多维数组。此函数将帮助您获取数组的name元素,并允许您指定不同的id值。

function findKey($bigArray, $idxVal) {
    foreach($bigArray as $array) {
        if(array_key_exists('id', $array) && $array['id'] == $idxVal) {
            return $array['name'];
        }
    }
    return false;
}

//Supply your array for $arr
print(findKey($arr, '2')); //"chouette"

#6


0  

It's a bit crude, but this would get you the name...

它有点粗糙,但这会让你得到这个名字......

$name = false;
foreach($arr as $v) {
    if($v['id'] == '2') {
         $name = $v['name'];
         break;
    }   
}
echo $name;

So no, you are not forced to reindex the array, but it would make things easier.

所以不,你没有*重新索引数组,但它会让事情变得更容易。