duanbaque4230 2017-04-12 20:52
浏览 27
已采纳

快速搜索PHP数组

I have an array with around 3500 array items inside it, the format being below. I have two dynamic variables that change and I need to search the array to find out row price_value based on already knowing price_slot and country.

I've currently got the below but this is taking too long. Is there anyway I can speed up this access?

PHP Function

$country = US;
$priceSlot = 3;
$priceValue = getPriceValue($priceSlot, $country);


function getPriceValue($priceSlot, $country) {
    // Search in array for price
    foreach ($array as $arrayItem) {
        if ($arrayItem['price_slot'] == $priceSlot && $arrayItem['country'] == $country) {
            return $arrayItem['price_value'];
        }
    }
    return null;
}

Snippet of Array

Array
(
    [0] => Array
        (
            [price_slot] => 1
            [base_price] => Get in Touch
            [country] => US
            [price_multiplier] => 
            [price_value] => Get in Touch
        )

    [1] => Array
        (
            [price_slot] => 2
            [base_price] => 9000
            [country] => US
            [price_multiplier] => 1.3
            [price_value] => 11700
        )

    [2] => Array
        (
            [price_slot] => 3
            [base_price] => 12000
            [country] => US
            [price_multiplier] => 1.3
            [price_value] => 15600
        )

    [3] => Array
        (
            [price_slot] => 4
            [base_price] => 15000
            [country] => US
            [price_multiplier] => 1.3
            [price_value] => 19500
        )

    [4] => Array
        (
            [price_slot] => 5
            [base_price] => 4000
            [country] => US
            [price_multiplier] => 1.3
            [price_value] => 5200
        )

    [5] => Array
        (
            [price_slot] => 6
            [base_price] => 1600
            [country] => US
            [price_multiplier] => 1.3
            [price_value] => 2080
        )

Is there any faster way to get around this?

Thanks!

  • 写回答

3条回答 默认 最新

  • doushen4719 2017-04-12 21:09
    关注

    I'm still trying to think of another way, but this is faster:

    $result = array_filter($array, function($v) use($priceSlot, $country) {
                                       return ($v['price_slot'] == $priceSlot && 
                                               $v['country']    == $country);
                                   });
    

    Then you would need to access:

    echo current($result)['price_value'];
    

    You can get the price_value in $result like this:

    array_filter($array, function($v) use(&$result, $priceSlot, $country) {
                             $result = ($v['price_slot'] == $priceSlot &&
                                        $v['country']    == $country) ?
                                        $v['price_value'] : null;
                         });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端