doze79040 2011-11-17 23:21
浏览 71
已采纳

遍历数组的强大方法

I had the data in XML file i.e.

<domain>  
   <host>xyz</host>  
   <key>keeeeeeeeeey</key>  
</domain>
<domain>
  <host>xyz</host>  
   <key>keeeeeeeeeey</key> 
</domain>

From that xml I created an array for robustness had I knew how to find that using xml I would have done so but lack of my knowledge I converted that xml file into array using:

 $json = json_encode($xml); 
 $array = json_decode($json,TRUE);`

Below is my array:

Array
(
    [domain] => Array
        (
            [0] => Array
                (
                    [host] => bdbdfbdvbdbdfbdfbf.net
                    [key] => 933f416350de1a955544b30b5bb7ca09cfa2311101a22972320cc4c7af2ecedc03f36b8a48961ef938972478592a1e261819052b51c09a45cf805663f83cb2c0233969255a2c3e2e7e212a295a247b785d41
                )

            [1] => Array
                (
                    [host] => bdev1vvvvvvveinf.net
                    [key] => 933f416350de1a955544b30b5bb7ca09cfa2311101a22972320cc4c7af2ecedc03f36b8a48961ef938972478592a1e261819052b51c09a45cf805663f83cb2c0233969255a2c3e2e7e212a295a247b785d41
                )

            [2] => Array
                (
                    [host] => bdev1.aaaaaaaaureinf.net
                    [key] => 933f416350de1a955544b30b5bb7ca09cfa2311101a22972320cc4c7af2ecedc03f36b8a48961ef938972478592a1e261819052b51c09a45cf805663f83cb2c0233969255a2c3e2e7e212a295a247b785d41
                )

            [3] => Array
                (
                    [host] => bdennnnnnnninf.net
                    [key] => 933f416350de1a955544b30b5bb7ca09cfa2311101a22972320cc4c7af2ecedc03f36b8a48961ef938972478592a1e261819052b51c09a45cf805663f83cb2c0233969255a2c3e2e7e212a295a247b785d41
                )

            [4] => Array
                (
                    [host] => bdeveewerwerwerwerreinf.net
                    [key] => 933f416350de1a955544b30b5bb7ca09cfa2311101a22972320cc4c7af2ecedc03f36b8a48961ef938972478592a1e261819052b51c09a45cf805663f83cb2c0233969255a2c3e2e7e212a295a247b785d41
                )

        )

)

I want a robust loop through which if I pass the host name it returns the key. Can anyone please throw some light? The size of the array could grow to 100 000 000 plus entries.

  • 写回答

5条回答 默认 最新

  • duanbei1598 2011-11-17 23:25
    关注

    Change your array to look like:

    $data = array('domain' => array(
      'bdbdfbdvbdbdfbdfbf.net' => '933...',
      'bdev1vvvvvvveinf.net' => '933...',
    ));
    

    Then you can do this:

    echo $data['domain']['bdbdfbdvbdbdfbdfbf.net'];
    

    There is no "robust" way to do it with your current array. You'd have to search through the entire thing:

    function get_key($data, $host)
    {
        foreach ($data['domain'] as $domain)
        {
          if ($domain['host'] == $host)
            return $domain['key'];
        }
    }
    

    Given this new information:

    The size of the array could grow to 100 000 000 plus entries.

    I retract the usefulness of this answer, as the entire concept of using any plain text format, including XML or a serialized PHP key-value array, to store this amount of data is just crazy.

    You should store the data in a database with the domain indexed. Even an sqlite database would be a major upgrade from a linear probe of a text file.

    Of course there are ways to store the data in a custom format that is optimized, but there's really no good reason to reinvent something a database can easily do.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化