简体   繁体   English

我的阵列设计应该如何?

[英]How should my array design look like?

I think I messed up with my array design. 我想我弄乱了我的阵列设计。

It currenlty looks like this: 它看起来像这样:

 [new york] => Array
        (
            [id] => up
            [cinemas] => Array
                (
                    [AMC Loews 34th Street 14] => Array
                        (
                            [id] => 83
                            [lounges] => Array
                                (
                                    [FS 1] => 52
                                    [FS 2] => 70
                                )
                        )

                    [AMC Newport Centre 11] => Array
                        (
                            [id] => 159
                            [lounges] => Array
                                (
                                    [Royal 1] => 163
                                    [Royal 2] => 71
                                )
                        )

I thought I was smart when I sorted it like this. 当我这样排序时,我以为自己很聪明。 But it seems hard to fetch data from the array. 但是似乎很难从数组中获取数据。 I will always have the city name , in this ex new york . 在这个前new york ,我将永远有这个城市的名字 Fetching id for new york is going ok. 取得纽约的id可以。

But say that for example I have id 83, and want to fetch name AMC Loews 34th Street 14 , how would i do that? 但是说,例如,我有id 83,并想获取名称AMC Loews 34th Street 14 ,我该怎么做?

I'd recommend to build a second array as an index, holding the id s as keys, so you can look up the corresponding names: 我建议构建第二个数组作为索引,将id保留为键,以便您可以查找对应的名称:

$lookup = array(83 => 'AMC Loews 34th Street 14', 
                159 => 'AMC Newport Centre 11');

Then you can look up like this: 然后,您可以像这样查找:

$array[$city]['cinemas'][$lookup[$id]];

On the other hand you might be better off to redesign the array alltogether. 另一方面,最好一起重新设计阵列。 It depends on what the majority of your reads consists of. 这取决于您阅读的大部分内容。 Do you usually have the names and citys or rather the id numbers? 您通常有名字和城市,或者是身份证号码吗? The other possibility is always to search with a loop, maybe like this: 另一种可能性是始终使用循环搜索,也许像这样:

foreach($array[$city]['cinemas'] as $name => $cin){
    if($name == $searchstring){
        $result = $cin['id'];
        break;
    }
}

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

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