Php优化双循环(数组和对象)

时间:2018-03-16 08:51:02

标签: php optimization

我需要在循环(对象)中优化循环(数组)。下面是我的解决方案,但是如果我用大量数据尝试这个,那就太慢了。

这是我的数组

$data = [
      ["PLZ", "Preis"],
      ["8074", "90"],
      ["8075", "90"],
      ["8076", "90"],
      ["8077", "90"],
      ["8078", "77"],
      ["1010", "77"],
      ["1020", "77"],
      ["1030", "77"],
      ["8041", "55"],
      ["8020", "89"],
    ];

这是我的对象

$postal_collection = {
"1010":1,
"1020":2,
"1030":3,
"8020":1602,
"8041":1604,
"8074":1622,
"8075":1623,
"8076":1624,
"8077":1625
}

这是工作循环

$allData = [];
    foreach ($data as $key => $fields) {
      foreach ($postal_collection as $postal => $placeId) {
        if ($fields[0] == $postal) {
          $allData[$placeId] = [
            'postal' => $postal,
            'place_id' => $placeId,
            'price' => $fields[1],
          ];
        }
      }
    }

那么我怎样才能改变这个循环来做同样的工作但更快?

2 个答案:

答案 0 :(得分:3)

您可以使用$postal_collection

的键来避免一个foreach
$allData = [];
foreach ($data as $key => $fields) {
    $id = $fields[0] ;
    // check if the key exists in $postal_collection:
    if (!isset($postal_collection[$id])) continue ;
    // get the value
    $cp = $postal_collection[$id];
    // add to $allData
    $allData[$cp] = [
        'postal' => $id,
        'place_id' => $cp,
        'price' => $fields[1],
      ];
}
print_r($allData);

输出:

Array
(
    [1622] => Array
        (
            [postal] => 8074
            [place_id] => 1622
            [price] => 90
        )

    [1623] => Array
        (
            [postal] => 8075
            [place_id] => 1623
            [price] => 90
        )

    [1624] => Array
        (
            [postal] => 8076
            [place_id] => 1624
            [price] => 90
        )

    [1625] => Array
        (
            [postal] => 8077
            [place_id] => 1625
            [price] => 90
        )

    [1] => Array
        (
            [postal] => 1010
            [place_id] => 1
            [price] => 77
        )

    [2] => Array
        (
            [postal] => 1020
            [place_id] => 2
            [price] => 77
        )

    [3] => Array
        (
            [postal] => 1030
            [place_id] => 3
            [price] => 77
        )

    [1604] => Array
        (
            [postal] => 8041
            [place_id] => 1604
            [price] => 55
        )

    [1602] => Array
        (
            [postal] => 8020
            [place_id] => 1602
            [price] => 89
        )

)

答案 1 :(得分:0)

如果$ fields [0](每个$ data“row”中的第一项)是唯一的,您可以循环访问这些并创建一个查找数组。从该查找数组中分配单个项目将很快。

然后你可以循环遍历$ postal_collection并只在一次传递中创建你的$ all_data结果。

$lookup = [];    
foreach ($data as $row){
    $lookup[$row[0]] = $row[1];
}

$allData = [];
foreach ($postal_collection as $postal => $placeId) {
    if (isset($lookup[$postal]) && !isset($allData[$placeId])){
        $allData[$placeId] = [
            'postal' => $postal,
            'place_id' => $placeId,
            'price' => $lookup[$postal]
         ];
     }
}