简体   繁体   English

通过php中的值查找多维关联数组的键

[英]Find key of a multi-dimensional associative array by value in php

I am trying to find the key of a multi-dimensional array using array functions (without looping the entire array set). 我正在尝试使用数组函数(不循环整个数组集)来查找多维数组的键。

My array is like below, 我的数组如下

$main_array = [];

$main_array[0]['id']=1001;
$main_array[0]['name']=test1;
$main_array[1]['id']=1002;
$main_array[1]['name']=test2;
$main_array[2]['id']=1001;
$main_array[2]['name']=test3;

I want to get the index of array by using the value without looping all the array (because my array is bit huge). 我想通过使用该值来获取数组的索引,而无需循环所有数组(因为我的数组有点大)。

If I pass the value "1001", I want the two index 0 and 2. Tried with array_search() function, not working in my case. 如果我传递值“ 1001”,则我希望两个索引分别为0和2。尝试使用array_search()函数,在我的情况下不起作用。

Please help me to solve this issue. 请帮我解决这个问题。

I dont think you could avoid completely from searching the array. 我认为您无法完全避免搜索数组。

your structure is not planned well. 您的结构规划不好。 you should have used the id (which should be unique) as keys for your array like so: 您应该使用id(应该是唯一的)作为数组的键,如下所示:

$main_array[1001]['name'] = 'foo'

this would be much easier to handle and to maintain. 这将更容易处理和维护。

I suggest you to make an effort and change your structure before it gets really big. 我建议您在结构变大之前做出努力并更改结构。

You should consider changing structure of array . 您应该考虑更改array结构。 As your ID is not unique, elements with same ID stay in one array . 由于您的ID不是唯一的,因此具有相同ID的元素将保留在一个array

$main_array = array(
    1001 => array(
        array('name' => 'test1'),
        array('name' => 'test3'),
    ),
    1002 => array(
        array('name' => 'test2'),
    )
);

So 所以

print_r( $main_array[1001] );

would give you 会给你

Array
(
    [0] => Array
        (
            [name] => test1
        )

    [1] => Array
        (
            [name] => test3
        )

)

If this is not possible, you have to loop over the entire array to achieve this. 如果不可能,则必须遍历整个阵列以实现此目的。

function arraySearchId( $id, $array ) {
    $results = array();
    foreach ( $array as $key => $val ) {
        if ( $val['id'] === $id ) {
            $results[] = $key;
        }
    }
    return $results;
}

echo '<pre>';
print_r( arraySearchId( 1001, $main_array ) );
echo '</pre>';

Result: 结果:

Array
(
    [0] => 0
    [1] => 2
)

As your code 作为你的代码

$main_array = [];
$main_array[0]['id']=1001;
$main_array[0]['name']=test1;
$main_array[1]['id']=1002;
$main_array[1]['name']=test2;
$main_array[2]['id']=1001;
$main_array[2]['name']=test3;

You require "Without looping", and the answer is: You must loop it at least once, to pre-process the data, make an indexed array. 您需要“无循环”,答案是:必须循环至少一次,以预处理数据并创建索引数组。

$indexed = array();
foreach($main_array as $i=>$item) {
    if( !isset($indexed[$item['id']]) ) $indexed[$item['id']] = array();
    $indexed[$item['id']][] = $i;
}

When you need to find 1001, just 当您需要找到1001时

$result = $indexed['1001'] // array(0,2)

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

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