Перебор двух массивов

Discussion in 'PHP' started by Seganapa, 26 Jul 2012.

  1. Seganapa

    Seganapa New Member

    Joined:
    8 Jun 2012
    Messages:
    74
    Likes Received:
    0
    Reputations:
    0
    Всем Добрый день!

    Имеется код:
    PHP:
    foreach ($city as $city1)
    {
    // По id города узнаем id острова
    $towninfo 'http://ru1.grepolis.com/game/town_info?action=info&town_id='.$city1['id'].'&h='.$token[1].'&json={"id":"'.$city1['id'].'","town_id":'.$city1['id'].',"nlreq_id":'.$nlreq[1].'}&_='.$timef;
    curl_setopt($curlCURLOPT_URL$towninfo);
    curl_setopt($curlCURLOPT_REFERER'http://ru1.grepolis.com/game/index?login=1');
    curl_setopt($curlCURLOPT_HTTPHEADER, array('X-REQUESTED-WITH: XMLHttpRequest'));
    curl_setopt($curlCURLOPT_POSTfalse);
    $html curl_exec($curl);
    //echo ($html);
    preg_match('#class="gp_island_link">.*\s(\d+)<\/a>#'$html$island);
    // id острова $island[1];
    //print_r ($island);
    echo 'CityID'.$city1['id'].' - '.'IslandID'.$island[1].'</br>';
    }
    Имеется определенное количество городов ($city['id']), каждый город находится на определенном острове ($island[1]), но случается так, что на одном острове может быть несколько городов...

    Задача в том, чтобы выбрать из тех городов, которые находятся на одном острове, один, с наименьшим кол-вом ресурсов...

    Не могу сообразить как это осуществить... Нужно склеить массивы или ???

    Вот что на выходе:
    HTML:
    CityID: 57664 - IslandID: 1146207
    CityID: 81453 - IslandID: 1146207
    CityID: 50380 - IslandID: 1146207
    CityID: 50278 - IslandID: 1146207
    CityID: 80290 - IslandID: 1146207
    CityID: 78218 - IslandID: 1146247
    CityID: 46826 - IslandID: 1146165
    CityID: 80121 - IslandID: 1143914
    CityID: 73254 - IslandID: 1146241
    CityID: 55584 - IslandID: 1143814
    CityID: 74000 - IslandID: 1146241
    CityID: 75350 - IslandID: 1146165
    CityID: 54128 - IslandID: 1146194
    
     
  2. Seganapa

    Seganapa New Member

    Joined:
    8 Jun 2012
    Messages:
    74
    Likes Received:
    0
    Reputations:
    0
    Спарсил сумму ресурсов, теперь код такой:
    PHP:
    foreach ($city as $city1)
    {
    // По id города узнаем id острова
    $towninfo 'http://ru1.grepolis.com/game/town_info?action=info&town_id='.$city1['id'].'&h='.$token[1].'&json={"id":"'.$city1['id'].'","town_id":'.$city1['id'].',"nlreq_id":'.$nlreq[1].'}&_='.$timef;
    curl_setopt($curlCURLOPT_URL$towninfo);
    curl_setopt($curlCURLOPT_REFERER'http://ru1.grepolis.com/game/index?login=1');
    curl_setopt($curlCURLOPT_HTTPHEADER, array('X-REQUESTED-WITH: XMLHttpRequest'));
    curl_setopt($curlCURLOPT_POSTfalse);
    $html curl_exec($curl);
    //echo ($html);
    preg_match('#class="gp_island_link">.*\s(\d+)<\/a>#'$html$island);

    // По id грода узнаем кол-во ресурсов и суммируем его
    $resourceinfourl 'http://ru1.grepolis.com/game/index?action=switch_town&town_id='.$city1['id'].'&h='.$token[1].'&json={"town_id":"'.$city1['id'].'","nlreq_id":'.$nlreq[1].'}&_='.$timef;
    curl_setopt($curlCURLOPT_URL$resourceinfourl);
    curl_setopt($curlCURLOPT_REFERER'http://ru1.grepolis.com/game/index?login=1');
    curl_setopt($curlCURLOPT_HTTPHEADER, array('X-REQUESTED-WITH: XMLHttpRequest'));
    curl_setopt($curlCURLOPT_POSTfalse);
    $htmlres curl_exec($curl);
    //echo ($htmlres);
    preg_match ('#"resources":(.*?),"storage#'$htmlres$res);
    $res json_decode($res[1], true);
    $sumres array_sum ($res);
    //print_r ($sumres);

    echo 'CityID: '.$city1['id'].' - '.'IslandID: '.$island[1].' - '.'SumResource: '.$sumres.'</br>';

    На выходе получаю:
    HTML:
    CityID: 57664 - IslandID: 1146207 - SumResource: 65068
    CityID: 81453 - IslandID: 1146207 - SumResource: 65060
    CityID: 50380 - IslandID: 1146207 - SumResource: 54045
    CityID: 50278 - IslandID: 1146207 - SumResource: 65558
    CityID: 80290 - IslandID: 1146207 - SumResource: 65822
    CityID: 78218 - IslandID: 1146247 - SumResource: 64935
    CityID: 46826 - IslandID: 1146165 - SumResource: 66131
    CityID: 80121 - IslandID: 1143914 - SumResource: 57208
    CityID: 73254 - IslandID: 1146241 - SumResource: 53561
    CityID: 55584 - IslandID: 1143814 - SumResource: 52142
    CityID: 74000 - IslandID: 1146241 - SumResource: 44035
    CityID: 75350 - IslandID: 1146165 - SumResource: 60809
    CityID: 54128 - IslandID: 1146194 - SumResource: 64693
    
    Т.е. я имею три массива
    1 - масиив с id городов
    2 - массив с id островов
    3. - массив с суммами ресурсов

    Мне нужно выбрать все города, но с таким условием, что если на одном острове имеется несколько городов из них оставить только тот, у которого сумма ресурсов меньше чем у остальных городов на этом острове...
     
  3. Seganapa

    Seganapa New Member

    Joined:
    8 Jun 2012
    Messages:
    74
    Likes Received:
    0
    Reputations:
    0
    Слил массивы и получил:
    HTML:
    Array
    (
        [0] => Array
            (
                [cityID] => 57664
                [islandID] => 1146207
                [SumRess] => 66413
            )
    
        [1] => Array
            (
                [cityID] => 81453
                [islandID] => 1146207
                [SumRess] => 66406
            )
    
        [2] => Array
            (
                [cityID] => 50380
                [islandID] => 1146207
                [SumRess] => 54843
            )
    
        [3] => Array
            (
                [cityID] => 50278
                [islandID] => 1146207
                [SumRess] => 66456
            )
    
        [4] => Array
            (
                [cityID] => 80290
                [islandID] => 1146207
                [SumRess] => 66720
            )
    
        [5] => Array
            (
                [cityID] => 78218
                [islandID] => 1146247
                [SumRess] => 65952
            )
    
        [6] => Array
            (
                [cityID] => 46826
                [islandID] => 1146165
                [SumRess] => 66482
            )
    
        [7] => Array
            (
                [cityID] => 80121
                [islandID] => 1143914
                [SumRess] => 58224
            )
    
        [8] => Array
            (
                [cityID] => 73254
                [islandID] => 1146241
                [SumRess] => 54361
            )
    
        [9] => Array
            (
                [cityID] => 55584
                [islandID] => 1143814
                [SumRess] => 10423
            )
    
        [10] => Array
            (
                [cityID] => 74000
                [islandID] => 1146241
                [SumRess] => 45380
            )
    
        [11] => Array
            (
                [cityID] => 75350
                [islandID] => 1146165
                [SumRess] => 61706
            )
    
        [12] => Array
            (
                [cityID] => 54128
                [islandID] => 1146194
                [SumRess] => 65807
            )
    
    )
    
    Как в это массиве выбрать все [cityID], но с условием, что если [islandID] совпадают, выбрать из них [cityID] в котором [SumRess] наименьший...

    Чёт никак не получается...
     
    #3 Seganapa, 26 Jul 2012
    Last edited: 26 Jul 2012