php – Hacer doble bucle para recorrer y extraer datos de un array


tengo un array que es bastante grande y necesito extraer la información de “citis” y “avatarScores”. La información de los avatarscores está relacionada con la información de citis, es decir los puntos que salen en avatarscores pertenecen a ciudades.

La ciudad 1 tiene los puntos que salen en la posición 1 de avatarscores. Las citis y avatarscores tambien se pueden relacionar por el ownerID que es un campo que se repite en ambos sitios arrays.

La pregunta es, como puedo unificar todo eso? Necesito juntar todos los datos que extraigo en un mismo array para luego poder guardarlo en una tabla de mysql.Tal y como lo hago ahora necesito 2 tablas

Este es el array

array(6) {
  (0)=>
  array(2) {
    (0)=>
    string(20) "updateBackgroundData"
    (1)=>
    object(stdClass)#2 (37) {
      ("id")=>
      string(2) "74"
      ("type")=>
      int(5)
      ("name")=>
      string(8) "Shaymios"
      ("xCoord")=>
      string(2) "44"
      ("yCoord")=>
      string(2) "47"
      ("tradegood")=>
      string(1) "3"
      ("tradegoodTarget")=>
      string(8) "noluxury"
      ("resourceLevel")=>
      string(2) "26"
      ("tradegoodLevel")=>
      string(2) "21"
      ("wonder")=>
      string(1) "2"
      ("wonderLevel")=>
      string(1) "4"
      ("wonderName")=>
      string(22) "Gruta Sagrada de Hades"
      ("showResourceWorkers")=>
      int(0)
      ("showTradegoodWorkers")=>
      int(0)
      ("showAgora")=>
      int(1)
      ("canEnterResource")=>
      int(1)
      ("canEnterTradegood")=>
      int(1)
      ("tradegoodEndUpgradeTime")=>
      int(0)
      ("resourceEndUpgradeTime")=>
      int(0)
      ("wonderEndUpgradeTime")=>
      int(0)
      ("isOwnCityOnIsland")=>
      bool(true)
      ("cities")=>
      array(17) {
        (0)=>
        object(stdClass)#3 (6) {
          ("id")=>
          int(-1)
          ("type")=>
          string(10) "buildplace"
          ("name")=>
          string(7) "Terreno"
          ("level")=>
          int(0)
          ("viewAble")=>
          int(1)
          ("buildplace_type")=>
          string(6) "normal"
        }
        (1)=>
        object(stdClass)#4 (13) {
          ("type")=>
          string(4) "city"
          ("name")=>
          string(7) "Nothing"
          ("id")=>
          int(64501)
          ("level")=>
          string(2) "10"
          ("ownerId")=>
          string(5) "27450"
          ("ownerName")=>
          string(7) "Paisano"
          ("ownerAllyId")=>
          string(3) "321"
          ("ownerAllyTag")=>
          string(5) "CROMA"
          ("hasTreaties")=>
          int(0)
          ("actions")=>
          array(0) {
          }
          ("state")=>
          string(0) ""
          ("viewAble")=>
          int(0)
          ("infestedByPlague")=>
          bool(false)
        }
        (2)=>
        object(stdClass)#5 (6) {
          ("id")=>
          int(-1)
          ("type")=>
          string(10) "buildplace"
          ("name")=>
          string(7) "Terreno"
          ("level")=>
          int(0)
          ("viewAble")=>
          int(1)
          ("buildplace_type")=>
          string(6) "normal"
        }
        (3)=>
        object(stdClass)#6 (6) {
          ("id")=>
          int(-1)
          ("type")=>
          string(10) "buildplace"
          ("name")=>
          string(7) "Terreno"
          ("level")=>
          int(0)
          ("viewAble")=>
          int(1)
          ("buildplace_type")=>
          string(6) "normal"
        }
        (4)=>
        object(stdClass)#7 (12) {
          ("type")=>
          string(4) "city"
          ("name")=>
          string(5) "Polis"
          ("id")=>
          int(64437)
          ("level")=>
          string(1) "1"
          ("ownerId")=>
          string(5) "28312"
          ("ownerName")=>
          string(14) "Agamemnon-3798"
          ("ownerAllyId")=>
          string(1) "0"
          ("hasTreaties")=>
          int(0)
          ("actions")=>
          array(0) {
          }
          ("state")=>
          string(0) ""
          ("viewAble")=>
          int(2)
          ("infestedByPlague")=>
          bool(false)
        }
        (5)=>
        object(stdClass)#8 (13) {
          ("type")=>
          string(4) "city"
          ("name")=>
          string(9) "Evaroschi"
          ("id")=>
          int(5405)
          ("level")=>
          string(2) "26"
          ("ownerId")=>
          string(4) "3739"
          ("ownerName")=>
          string(6) "cesar2"
          ("ownerAllyId")=>
          string(3) "324"
          ("ownerAllyTag")=>
          string(4) "-T1-"
          ("hasTreaties")=>
          int(0)
          ("actions")=>
          array(0) {
          }
          ("state")=>
          string(0) ""
          ("viewAble")=>
          int(0)
          ("infestedByPlague")=>
          bool(false)
        }
        (6)=>
        object(stdClass)#9 (6) {
          ("id")=>
          int(-1)
          ("type")=>
          string(10) "buildplace"
          ("name")=>
          string(7) "Terreno"
          ("level")=>
          int(0)
          ("viewAble")=>
          int(1)
          ("buildplace_type")=>
          string(6) "normal"
        }
        (7)=>
        object(stdClass)#10 (13) {
          ("type")=>
          string(4) "city"
          ("name")=>
          string(9) "Cristal I"
          ("id")=>
          int(849)
          ("level")=>
          string(2) "27"
          ("ownerId")=>
          string(3) "849"
          ("ownerName")=>
          string(7) "DaniloJ"
          ("ownerAllyId")=>
          string(1) "2"
          ("ownerAllyTag")=>
          string(4) "-ZT-"
          ("hasTreaties")=>
          int(0)
          ("actions")=>
          array(0) {
          }
          ("state")=>
          string(0) ""
          ("viewAble")=>
          int(0)
          ("infestedByPlague")=>
          bool(false)
        }
        (8)=>
        object(stdClass)#11 (6) {
          ("id")=>
          int(-1)
          ("type")=>
          string(10) "buildplace"
          ("name")=>
          string(7) "Terreno"
          ("level")=>
          int(0)
          ("viewAble")=>
          int(1)
          ("buildplace_type")=>
          string(6) "normal"
        }
        (9)=>
        object(stdClass)#12 (12) {
          ("type")=>
          string(4) "city"
          ("name")=>
          string(5) "Polis"
          ("id")=>
          int(64892)
          ("level")=>
          string(1) "1"
          ("ownerId")=>
          string(5) "28488"
          ("ownerName")=>
          string(8) "-NAGATO-"
          ("ownerAllyId")=>
          string(1) "0"
          ("hasTreaties")=>
          int(0)
          ("actions")=>
          array(0) {
          }
          ("state")=>
          string(8) "inactive"
          ("viewAble")=>
          int(0)
          ("infestedByPlague")=>
          bool(false)
        }
        (10)=>
        object(stdClass)#13 (13) {
          ("type")=>
          string(4) "city"
          ("name")=>
          string(4) "0001"
          ("id")=>
          int(40246)
          ("level")=>
          string(2) "20"
          ("ownerId")=>
          string(5) "18230"
          ("ownerName")=>
          string(13) "NeWRomerOrdeR"
          ("ownerAllyId")=>
          string(3) "307"
          ("ownerAllyTag")=>
          string(4) "ROMA"
          ("hasTreaties")=>
          int(0)
          ("actions")=>
          array(0) {
          }
          ("state")=>
          string(0) ""
          ("viewAble")=>
          int(0)
          ("infestedByPlague")=>
          bool(false)
        }
        (11)=>
        object(stdClass)#14 (12) {
          ("type")=>
          string(4) "city"
          ("name")=>
          string(5) "Polis"
          ("id")=>
          int(64942)
          ("level")=>
          string(1) "1"
          ("ownerId")=>
          string(5) "28503"
          ("ownerName")=>
          string(13) "AnitaFacilita"
          ("ownerAllyId")=>
          string(1) "0"
          ("hasTreaties")=>
          int(0)
          ("actions")=>
          array(0) {
          }
          ("state")=>
          string(8) "inactive"
          ("viewAble")=>
          int(0)
          ("infestedByPlague")=>
          bool(false)
        }
        (12)=>
        object(stdClass)#15 (12) {
          ("type")=>
          string(4) "city"
          ("name")=>
          string(5) "Polis"
          ("id")=>
          int(64988)
          ("level")=>
          string(1) "1"
          ("ownerId")=>
          string(5) "28523"
          ("ownerName")=>
          string(9) "Sklavidel"
          ("ownerAllyId")=>
          string(1) "0"
          ("hasTreaties")=>
          int(0)
          ("actions")=>
          array(0) {
          }
          ("state")=>
          string(4) "noob"
          ("viewAble")=>
          int(0)
          ("infestedByPlague")=>
          bool(false)
        }
        (13)=>
        object(stdClass)#16 (12) {
          ("type")=>
          string(4) "city"
          ("name")=>
          string(5) "Polis"
          ("id")=>
          int(63482)
          ("level")=>
          string(2) "14"
          ("ownerId")=>
          string(5) "27754"
          ("ownerName")=>
          string(10) "OL-Fredy97"
          ("ownerAllyId")=>
          string(1) "0"
          ("hasTreaties")=>
          int(0)
          ("actions")=>
          array(0) {
          }
          ("state")=>
          string(0) ""
          ("viewAble")=>
          int(0)
          ("infestedByPlague")=>
          bool(false)
        }
        (14)=>
        object(stdClass)#17 (6) {
          ("id")=>
          int(-1)
          ("type")=>
          string(10) "buildplace"
          ("name")=>
          string(7) "Terreno"
          ("level")=>
          int(0)
          ("viewAble")=>
          int(1)
          ("buildplace_type")=>
          string(6) "normal"
        }
        (15)=>
        object(stdClass)#18 (13) {
          ("type")=>
          string(4) "city"
          ("name")=>
          string(6) "Atenas"
          ("id")=>
          int(32646)
          ("level")=>
          string(2) "24"
          ("ownerId")=>
          string(5) "14700"
          ("ownerName")=>
          string(7) "Charles"
          ("ownerAllyId")=>
          string(3) "307"
          ("ownerAllyTag")=>
          string(4) "ROMA"
          ("hasTreaties")=>
          int(0)
          ("actions")=>
          array(0) {
          }
          ("state")=>
          string(0) ""
          ("viewAble")=>
          int(0)
          ("infestedByPlague")=>
          bool(false)
        }
        (16)=>
        object(stdClass)#19 (6) {
          ("id")=>
          int(-1)
          ("type")=>
          string(10) "buildplace"
          ("name")=>
          string(7) "Terreno"
          ("level")=>
          int(0)
          ("viewAble")=>
          int(1)
          ("buildplace_type")=>
          string(7) "premium"
        }
      }
      ("barbarians")=>
      object(stdClass)#20 (11) {
        ("invisible")=>
        int(0)
        ("actionTitle")=>
        string(55) "¡Ninguna tropa/barco mercante disponible para saquear!"
        ("actionClass")=>
        string(19) "plundering disabled"
        ("actionLink")=>
        string(0) ""
        ("count")=>
        int(1)
        ("wallLevel")=>
        int(0)
        ("level")=>
        int(1)
        ("underAttack")=>
        int(0)
        ("isTradegoodSiege")=>
        int(0)
        ("city")=>
        string(8) "kingOlaf"
        ("destroyed")=>
        int(0)
      }
      ("avatarScores")=>
      object(stdClass)#22 (10) {
        ("849")=>
        object(stdClass)#21 (6) {
          ("avatar_id")=>
          string(3) "849"
          ("place")=>
          string(1) "7"
          ("building_score_main")=>
          string(11) "379,322,313"
          ("research_score_main")=>
          string(10) "72,145,880"
          ("army_score_main")=>
          string(7) "250,190"
          ("trader_score_secondary")=>
          string(10) "12,848,160"
        }
        ("3739")=>
        object(stdClass)#23 (6) {
          ("avatar_id")=>
          string(5) "3,739"
          ("place")=>
          string(3) "165"
          ("building_score_main")=>
          string(10) "60,479,333"
          ("research_score_main")=>
          string(9) "7,276,760"
          ("army_score_main")=>
          string(7) "199,960"
          ("trader_score_secondary")=>
          string(9) "4,120,270"
        }
        ("14700")=>
        object(stdClass)#24 (6) {
          ("avatar_id")=>
          string(6) "14,700"
          ("place")=>
          string(3) "237"
          ("building_score_main")=>
          string(10) "24,048,522"
          ("research_score_main")=>
          string(9) "7,276,760"
          ("army_score_main")=>
          string(7) "380,000"
          ("trader_score_secondary")=>
          string(9) "7,027,608"
        }
        ("18230")=>
        object(stdClass)#25 (6) {
          ("avatar_id")=>
          string(6) "18,230"
          ("place")=>
          string(3) "243"
          ("building_score_main")=>
          string(10) "26,427,220"
          ("research_score_main")=>
          string(9) "3,953,400"
          ("army_score_main")=>
          string(7) "472,620"
          ("trader_score_secondary")=>
          string(9) "2,657,737"
        }
        ("27450")=>
        object(stdClass)#26 (6) {
          ("avatar_id")=>
          string(6) "27,450"
          ("place")=>
          string(3) "334"
          ("building_score_main")=>
          string(9) "6,161,438"
          ("research_score_main")=>
          string(7) "891,972"
          ("army_score_main")=>
          string(7) "144,860"
          ("trader_score_secondary")=>
          string(6) "41,764"
        }
        ("27754")=>
        object(stdClass)#27 (6) {
          ("avatar_id")=>
          string(6) "27,754"
          ("place")=>
          string(3) "349"
          ("building_score_main")=>
          string(9) "4,536,533"
          ("research_score_main")=>
          string(7) "702,972"
          ("army_score_main")=>
          string(6) "20,660"
          ("trader_score_secondary")=>
          string(7) "125,021"
        }
        ("28312")=>
        object(stdClass)#28 (6) {
          ("avatar_id")=>
          string(6) "28,312"
          ("place")=>
          string(3) "551"
          ("building_score_main")=>
          string(1) "0"
          ("research_score_main")=>
          string(1) "0"
          ("army_score_main")=>
          string(1) "0"
          ("trader_score_secondary")=>
          string(6) "61,843"
        }
        ("28488")=>
        object(stdClass)#29 (6) {
          ("avatar_id")=>
          string(6) "28,488"
          ("place")=>
          string(3) "552"
          ("building_score_main")=>
          string(1) "0"
          ("research_score_main")=>
          string(1) "0"
          ("army_score_main")=>
          string(1) "0"
          ("trader_score_secondary")=>
          string(6) "18,439"
        }
        ("28503")=>
        object(stdClass)#30 (6) {
          ("avatar_id")=>
          string(6) "28,503"
          ("place")=>
          string(3) "555"
          ("building_score_main")=>
          string(1) "0"
          ("research_score_main")=>
          string(1) "0"
          ("army_score_main")=>
          string(1) "0"
          ("trader_score_secondary")=>
          string(6) "14,262"
        }
        ("28523")=>
        object(stdClass)#31 (6) {
          ("avatar_id")=>
          string(6) "28,523"
          ("place")=>
          string(3) "498"
          ("building_score_main")=>
          string(1) "0"
          ("research_score_main")=>
          string(1) "0"
          ("army_score_main")=>
          string(3) "360"
          ("trader_score_secondary")=>
          string(6) "10,879"
        }
      }
      ("specialServerBadges")=>
      array(0) {
      }
      ("selectedCityParameters")=>
      array(0) {
      }
      ("island")=>
      string(2) "74"
      ("isHeliosTowerBuilt")=>
      bool(false)
      ("heliosTop")=>
      int(1)
      ("heliosMid")=>
      int(1)
      ("heliosBase")=>
      int(1)
      ("heliosName")=>
      string(15) "Ruinas de torre"
      ("heliosTooltip")=>
      string(24) "Torre de Helios inactiva"
      ("heliosActive")=>
      int(0)
      ("showResourceBonusIcon")=>
      int(0)
      ("showTradegoodBonusIcon")=>
      int(0)
      ("walkers")=>
      object(stdClass)#32 (1) {
        ("add")=>
        array(0) {
        }
      }
    }
  }
  (1)=>
  array(2) {
    (0)=>
    string(18) "updateTemplateData"
    (1)=>
    string(0) ""
  }
  (2)=>
  array(2) {
    (0)=>
    string(9) "popupData"
    (1)=>
    NULL
  }
  (3)=>
  array(2) {
    (0)=>
    string(17) "ingameCounterData"
    (1)=>
    NULL
  }
  (4)=>
  array(2) {
    (0)=>
    string(23) "removeIngameCounterData"
    (1)=>
    NULL
  }
  (5)=>
  array(2) {
    (0)=>
    string(14) "updateBacklink"
    (1)=>
    NULL
  }
}

Este el codigo para extraer los datos de citis

$owners = ();
foreach($final(0)(1)->cities as $city){

$owner = "";
$allyTag = "";
$namecity = "";
$citylevel ="";
$idowner = "";

if(isset($city->ownerName)){
    $owner = $city->ownerName;
if(isset($city->ownerId)){
    $idowner = $city->ownerId;  
if (isset($city->name)) {
    $namecity = $city->name;
if (isset($city->level)) {
    $citylevel = $city->level;
if(isset($city->ownerAllyTag)){
    $allyTag = $city->ownerAllyTag;
}

array_push($owners, ("idowner"=>$idowner, "citylevel"=>$citylevel, "owner"=>$owner, "allyTag" => $allyTag, "namecity" => $namecity));


}}}}

}

y tengo este codigo para los avatar scores

foreach($final(0)(1)->avatarScores as $avatarScores){
$vavatar_id = "";
$vplace = "";
$vbuilding_score_main = "";
$vresearch_score_main = "";
$varmy_score_main = "";
$vtrader_score_secondary = "";

if(isset($scores->avatar_id)){
    $vavatar_id = $scores->avatar_id;
}
if(isset($scores->place)){
    $vplace = $scores->place;
}
if(isset($scores->building_score_main)){
    $vbuilding_score_main = $scores->building_score_main;
}
if(isset($scores->research_score_main)){
    $vresearch_score_main = $scores->research_score_main;
}
if(isset($scores->army_score_main)){
    $varmy_score_main = $scores->army_score_main;
}
if(isset($scores->trader_score_secondary)){
    $vtrader_score_secondary = $scores->trader_score_secondary;
}

array_push($final, ("avatar_id"=>$vavatar_id,"place"=>$vplace,"building_score_main"=>$vbuilding_score_main,"research_score_main"=>$vresearch_score_main,"army_score_main"=>$varmy_score_main,"trader_score_secondary"=>$vtrader_score_secondary ));
}