I have an array that is quite large. I need to extract the information from the "citis" and "avatarScores".The information of avatarscores is related to the information of citis.
For example city 1 belongs to player 1 who has the points that appear in avatarscores.
The citis array is always 17 places, but there does not have to be 17 cities.
The array of avatarscores is the size that cities actually exist.
The question is how can I do a double foreach, to extract the information from both arrays, and that they associate well so that the information of each city is correct.
I was doing a lot of testing, but duplicate records were continually coming out.There are only 10 cities in that array, and I always got about 100 cities (duplicate information)
The 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
}
}
code to extract
$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]);
}}}}
}
code from avatarscores that has to be unified with the one above
$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 ]);
}
Loop through the avatarScores array, then search for the key in the idowner field of the $owners array.
foreach (foreach($final[0][1]->avatarScores as $avatarOwner => $scores) {
$vavatar_id = "";
$vplace = "";
$vbuilding_score_main = "";
$vresearch_score_main = "";
$varmy_score_main = "";
$vtrader_score_secondary = "";
$city = null;
foreach ($owners as $owner) {
if ($owner['idowner'] == $avatarOwner) {
$city = $owner;
break;
}
}
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;
}
}
Related
I've google sheet with this data:
I'm pulling it from google drive with simple code:
$ranges = [
'Price_1'
];
$params = array(
'ranges' => $ranges
);
$response = $service->spreadsheets_values->batchGet($spreadsheetId, $params);
The response is a complicated object:
object(Google_Service_Sheets_BatchGetValuesResponse)#58 (8) { ["collection_key":protected]=> string(11) "valueRanges" ["spreadsheetId"]=> string(44) "1BQpKLuvlaVeGqIqoGUOynF9IZ5C3zK6gagZiRJO1UEU" ["valueRangesType":protected]=> string(32) "Google_Service_Sheets_ValueRange" ["valueRangesDataType":protected]=> string(5) "array" ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } ["valueRanges"]=> array(1) { [0]=> object(Google_Service_Sheets_ValueRange)#67 (7) { ["collection_key":protected]=> string(6) "values" ["majorDimension"]=> string(4) "ROWS" ["range"]=> string(14) "Price_1!E2:E74" ["values"]=> array(65) { [0]=> array(1) { [0]=> string(2) "28" } [1]=> array(1) { [0]=> string(2) "14" } [2]=> array(1) { [0]=> string(1) "0" } [3]=> array(0) { } [4]=> array(1) { [0]=> string(2) "55" } [5]=> array(1) { [0]=> string(2) "28" } [6]=> array(1) { [0]=> string(1) "0" } [7]=> array(0) { } [8]=> array(0) { } [9]=> array(0) { } [10]=> array(0) { } [11]=> array(0) { } [12]=> array(0) { } [13]=> array(0) { } [14]=> array(0) { } [15]=> array(0) { } [16]=> array(0) { } [17]=> array(0) { } [18]=> array(0) { } [19]=> array(0) { } [20]=> array(1) { [0]=> string(2) "59" } [21]=> array(1) { [0]=> string(2) "49" } [22]=> array(0) { } [23]=> array(1) { [0]=> string(2) "38" } [24]=> array(1) { [0]=> string(2) "38" } [25]=> array(0) { } [26]=> array(1) { [0]=> string(2) "37" } [27]=> array(1) { [0]=> string(2) "25" } [28]=> array(1) { [0]=> string(1) "0" } [29]=> array(0) { } [30]=> array(1) { [0]=> string(2) "79" } [31]=> array(1) { [0]=> string(2) "99" } [32]=> array(0) { } [33]=> array(1) { [0]=> string(2) "75" } [34]=> array(1) { [0]=> string(2) "99" } [35]=> array(0) { } [36]=> array(1) { [0]=> string(2) "79" } [37]=> array(1) { [0]=> string(2) "99" } [38]=> array(0) { } [39]=> array(1) { [0]=> string(3) "160" } [40]=> array(1) { [0]=> string(3) "190" } [41]=> array(0) { } [42]=> array(0) { } [43]=> array(0) { } [44]=> array(0) { } [45]=> array(0) { } [46]=> array(0) { } [47]=> array(0) { } [48]=> array(0) { } [49]=> array(0) { } [50]=> array(0) { } [51]=> array(0) { } [52]=> array(1) { [0]=> string(2) "33" } [53]=> array(0) { } [54]=> array(1) { [0]=> string(2) "59" } [55]=> array(1) { [0]=> string(2) "34" } [56]=> array(1) { [0]=> string(1) "0" } [57]=> array(0) { } [58]=> array(1) { [0]=> string(2) "29" } [59]=> array(1) { [0]=> string(2) "14" } [60]=> array(1) { [0]=> string(1) "0" } [61]=> array(0) { } [62]=> array(1) { [0]=> string(2) "37" } [63]=> array(1) { [0]=> string(2) "25" } [64]=> array(1) { [0]=> string(1) "0" } } ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } } } }
and var_dump($response->getValueRanges()[0]['values']); gives this result:
array(74) { [0]=> array(5) { [0]=> string(2) "ID" [1]=> string(10) "Ticket for" [2]=> string(9) "Excursion" [3]=> string(8) "Duration" [4]=> string(14) "Price (Brutto)" } [1]=> array(5) { [0]=> string(3) "01A" [1]=> string(13) "adult ( 12+ )" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(2) "28" } [2]=> array(5) { [0]=> string(3) "01B" [1]=> string(18) "child ( age 5-11 )" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(2) "14" } [3]=> array(5) { [0]=> string(3) "01C" [1]=> string(18) "toodler ( age 0-4)" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(1) "0" } [4]=> array(0) { } [5]=> array(5) { [0]=> string(3) "02A" [1]=> string(13) "adult, age 8+" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(2) "55" } [6]=> array(5) { [0]=> string(3) "02B" [1]=> string(14) "child, age 3-7" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(2) "28" } [7]=> array(5) { [0]=> string(3) "02C" [1]=> string(16) "toodler, age 0-2" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(1) "0" } [8]=> array(0) { } [9]=> array(3) { [0]=> string(3) "03A" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [10]=> array(3) { [0]=> string(3) "03B" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [11]=> array(3) { [0]=> string(3) "03C" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [12]=> array(0) { } [13]=> array(3) { [0]=> string(3) "04A" [1]=> string(0) "" [2]=> string(9) "Submarine" } [14]=> array(3) { [0]=> string(3) "04B" [1]=> string(0) "" [2]=> string(9) "Submarine" } [15]=> array(3) { [0]=> string(3) "04C" [1]=> string(0) "" [2]=> string(9) "Submarine" } [16]=> array(0) { } [17]=> array(3) { [0]=> string(3) "05A" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [18]=> array(3) { [0]=> string(3) "05B" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [19]=> array(3) { [0]=> string(3) "05C" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [20]=> array(0) { } [21]=> array(5) { [0]=> string(3) "06A" [1]=> string(9) "fisherman" [2]=> string(7) "Fishing" [3]=> string(2) "4h" [4]=> string(2) "59" } [22]=> array(5) { [0]=> string(3) "06B" [1]=> string(9) "spectator" [2]=> string(7) "Fishing" [3]=> string(2) "4h" [4]=> string(2) "49" } [23]=> array(0) { } [24]=> array(5) { [0]=> string(3) "07A" [1]=> string(5) "adult" [2]=> string(12) "Parascending" [3]=> string(5) "30min" [4]=> string(2) "38" } [25]=> array(5) { [0]=> string(3) "07B" [1]=> string(13) "child, age 6+" [2]=> string(12) "Parascending" [3]=> string(5) "30min" [4]=> string(2) "38" } [26]=> array(0) { } [27]=> array(5) { [0]=> string(3) "08A" [1]=> string(14) "adult, age 12+" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(2) "37" } [28]=> array(5) { [0]=> string(3) "08B" [1]=> string(15) "child, age 3-11" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(2) "25" } [29]=> array(5) { [0]=> string(3) "08C" [1]=> string(16) "toodler, age 0-1" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(1) "0" } [30]=> array(0) { } [31]=> array(5) { [0]=> string(3) "09A" [1]=> string(17) "flight from 800m." [2]=> string(18) "Paragliding tandem" [3]=> string(5) "15min" [4]=> string(2) "79" } [32]=> array(5) { [0]=> string(3) "09B" [1]=> string(18) "flight from 1000m." [2]=> string(18) "Paragliding tandem" [3]=> string(5) "25min" [4]=> string(2) "99" } [33]=> array(0) { } [34]=> array(5) { [0]=> string(3) "10A" [1]=> string(21) "Single (for 1 person)" [2]=> string(27) "Quad safari Forest explorer" [3]=> string(2) "3h" [4]=> string(2) "75" } [35]=> array(5) { [0]=> string(3) "10B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(27) "Quad safari Forest explorer" [3]=> string(2) "3h" [4]=> string(2) "99" } [36]=> array(0) { } [37]=> array(5) { [0]=> string(3) "11A" [1]=> string(21) "Single (for 1 person)" [2]=> string(24) "Quad Safari - Teide tour" [3]=> string(2) "4h" [4]=> string(2) "79" } [38]=> array(5) { [0]=> string(3) "11B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(24) "Quad Safari - Teide tour" [3]=> string(2) "4h" [4]=> string(2) "99" } [39]=> array(0) { } [40]=> array(5) { [0]=> string(3) "12A" [1]=> string(21) "Single (for 1 person)" [2]=> string(15) "Buggy adventure" [3]=> string(2) "3h" [4]=> string(3) "160" } [41]=> array(5) { [0]=> string(3) "12B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(15) "Buggy adventure" [3]=> string(2) "3h" [4]=> string(3) "190" } [42]=> array(0) { } [43]=> array(3) { [0]=> string(3) "13A" [1]=> string(0) "" [2]=> string(8) "Trekking" } [44]=> array(3) { [0]=> string(3) "13B" [1]=> string(0) "" [2]=> string(8) "Trekking" } [45]=> array(3) { [0]=> string(3) "13C" [1]=> string(0) "" [2]=> string(8) "Trekking" } [46]=> array(0) { } [47]=> array(3) { [0]=> string(3) "14A" [1]=> string(0) "" [2]=> string(7) "Karting" } [48]=> array(3) { [0]=> string(3) "14B" [1]=> string(0) "" [2]=> string(7) "Karting" } [49]=> array(3) { [0]=> string(3) "14C" [1]=> string(0) "" [2]=> string(7) "Karting" } [50]=> array(0) { } [51]=> array(3) { [0]=> string(3) "15A" [1]=> string(0) "" [2]=> string(11) "Jungle park" } [52]=> array(3) { [0]=> string(3) "15B" [1]=> string(0) "" [2]=> string(11) "Jungle park" } [53]=> array(5) { [0]=> string(3) "15C" [1]=> string(0) "" [2]=> string(11) "Jungle park" [3]=> string(0) "" [4]=> string(2) "33" } [54]=> array(0) { } [55]=> array(5) { [0]=> string(3) "16A" [1]=> string(14) "adult, age 12+" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(2) "59" } [56]=> array(5) { [0]=> string(3) "16B" [1]=> string(15) "child, age 2-11" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(2) "34" } [57]=> array(5) { [0]=> string(3) "16C" [1]=> string(16) "toodler, age 0-1" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(1) "0" } [58]=> array(0) { } [59]=> array(5) { [0]=> string(3) "17A" [1]=> string(14) "adult, age 12+" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(2) "29" } [60]=> array(5) { [0]=> string(3) "17B" [1]=> string(15) "child, age 2-11" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(2) "14" } [61]=> array(5) { [0]=> string(3) "17C" [1]=> string(16) "toodler, age 0-1" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(1) "0" } [62]=> array(0) { } [63]=> array(5) { [0]=> string(3) "18A" [1]=> string(14) "adult, age 12+" [2]=> string(11) "Loro parque" [3]=> string(12) "8:30 - 18:45" [4]=> string(2) "37" } [64]=> array(5) { [0]=> string(3) "18B" [1]=> string(15) "child, age 6-11" [2]=> string(11) "Loro parque" [3]=> string(0) "" [4]=> string(2) "25" } [65]=> array(5) { [0]=> string(3) "18C" [1]=> string(16) "toodler, age 0-1" [2]=> string(11) "Loro parque" [3]=> string(0) "" [4]=> string(1) "0" } [66]=> array(0) { } [67]=> array(3) { [0]=> string(3) "19A" [1]=> string(0) "" [2]=> string(9) "Antologia" } [68]=> array(3) { [0]=> string(3) "19B" [1]=> string(0) "" [2]=> string(9) "Antologia" } [69]=> array(3) { [0]=> string(3) "19C" [1]=> string(0) "" [2]=> string(9) "Antologia" } [70]=> array(0) { } [71]=> array(3) { [0]=> string(3) "20A" [1]=> string(0) "" [2]=> string(13) "Medieval show" } [72]=> array(3) { [0]=> string(3) "20B" [1]=> string(0) "" [2]=> string(13) "Medieval show" } [73]=> array(3) { [0]=> string(3) "20C" [1]=> string(0) "" [2]=> string(13) "Medieval show" } }
And:
for($i = 1; $i < 5; $i++) {
print_r ($response->getValueRanges()[0]['values'][$i]);
}
Gives: Array ( [0] => 01A [1] => adult ( 12+ ) [2] => Catamaran [3] => 2h [4] => 28 ) Array ( [0] => 01B [1] => child ( age 5-11 ) [2] => Catamaran [3] => 2h [4] => 14 ) Array ( [0] => 01C [1] => toodler ( age 0-4) [2] => Catamaran [3] => 2h [4] => 0 ) Array ( )
In result I need a simple array, which will hold a key[row id], the ID from from ID row and price from last row, somethig like this: $result[$i] = unique_id=1, ID=01A, price=28
How it could be achieved?
So after getting the data for google sheet with:
$arr = $response->getValueRanges()[0]['values'];
You can use this simple for loop to get the data as you need:
// notice start from 1 to avoid the headers
for($i = 1; $i < count($arr); $i++) {
$res[$i] = array("unique_id" => $i, "ID" => $arr[$i][0], "price" => $arr[$i][4]);
}
Now $res will contain you structure.
Here is the output of my program var_dump($myvar):
f_shopping array(2) {
["product-size"]=> array(3) {
[۰]=> object(WP_Term)#20766 (11) {
["term_id"]=> int(45)
["name"]=> string(8) "Large"
["slug"]=> string(5) "l"
["term_group"]=> int(0)
["term_taxonomy_id"]=> int(45)
["taxonomy"]=> string(13) "pa_product-size"
["description"]=> string(0) ""
["parent"]=> int(0)
["count"]=> int(2)
["filter"]=> string(3) "raw"
["meta_value"]=> string(1) "0"
}
[۱]=> object(WP_Term)#20791 (11) {
["term_id"]=> int(47)
["name"]=> string(8) "Small"
["slug"]=> string(5) "s"
["term_group"]=> int(0)
["term_taxonomy_id"]=> int(47)
["taxonomy"]=> string(13) "pa_product-size"
["description"]=> string(0) ""
["parent"]=> int(0)
["count"]=> int(2)
["filter"]=> string(3) "raw"
["meta_value"]=> string(1) "0"
}
[۲]=> object(WP_Term)#20780 (11) {
["term_id"]=> int(46)
["name"]=> string(10) "Medium"
["slug"]=> string(6) "m"
["term_group"]=> int(0)
["term_taxonomy_id"]=> int(46)
["taxonomy"]=> string(13) "pa_product-size"
["description"]=> string(0) ""
["parent"]=> int(0)
["count"]=> int(2)
["filter"]=> string(3) "raw"
["meta_value"]=> string(1) "0"
}
}
["shopping-pack"]=> array(4) {
[۰]=> object(WP_Term)#20751 (11) {
["term_id"]=> int(26)
["name"]=> string(13) "Box"
["slug"]=> string(3) "b"
["term_group"]=> int(0) ["term_taxonomy_id"]=> int(26)
["taxonomy"]=> string(16) "pa_shopping-pack"
["description"]=> string(0) ""
["parent"]=> int(0)
["count"]=> int(1)
["filter"]=> string(3) "raw"
["meta_value"]=> string(1) "1"
}
[۱]=> object(WP_Term)#20750 (11) {
["term_id"]=> int(25)
["name"]=> string(12) "Kilos"
["slug"]=> string(4) "k"
["term_group"]=> int(0)
["term_taxonomy_id"]=> int(25)
["taxonomy"]=> string(16) "pa_shopping-pack"
["description"]=> string(0) ""
["parent"]=> int(0) ["count"]=> int(1)
["filter"]=> string(3) "raw"
["meta_value"]=> string(1) "2"
}
[۲]=> object(WP_Term)#20749 (11) {
["term_id"]=> int(24)
["name"]=> string(13) "Single"
["slug"]=> string(6) "s"
["term_group"]=> int(0)
["term_taxonomy_id"]=> int(24)
["taxonomy"]=> string(16) "pa_shopping-pack"
["description"]=> string(0) ""
["parent"]=> int(0)
["count"]=> int(2)
["filter"]=> string(3) "raw"
["meta_value"]=> string(1) "3"
}
[۳]=> object(WP_Term)#20748 (11) {
["term_id"]=> int(73)
["name"]=> string(13) "Packed"
["slug"]=> string(6) "p"
["term_group"]=> int(0)
["term_taxonomy_id"]=> int(73)
["taxonomy"]=> string(16) "pa_shopping-pack"
["description"]=> string(0) ""
["parent"]=> int(0)
["count"]=> int(1)
["filter"]=> string(3) "raw"
["meta_value"]=> string(1) "4"
}
}
How can i store all ["name"]s inside of ["product-size"] and ["shopping-pack"] in two separate simple arrays with the values of "slug" as key and "name" as value?
I've searched before and tried. the solutions didn't work.
You can use array_column
$product = array_column($myVar['product-size'],'name','slug');
print_r($product);
$shopping = array_column($myVar['shopping-pack'],'name','slug');
print_r($shopping);
http://php.net/manual/en/function.array-column.php
Maybe this help to you:
$arr = "your array output";
$count_prod = count($arr['f_shopping']['product-size']);
$count_shopp = count($arr['f_shopping']['shopping-pack']);
$newArray = array();
for ($i = 0; $i < $count_prod; $i++) {
$slug = $arr['f_shopping']['product-size'][$i]['slug'];
$name = $arr['f_shopping']['product-size'][$i]['name'];
$newArray[]($slug -> $name);
}
for ($i = 0; $i < $count_shopp; $i++) {
$slug = $arr['f_shopping']['shopping-pack'][$i]['slug'];
$name = $arr['f_shopping']['shopping-pack'][$i]['name'];
$newArray[]($slug -> $name);
}
print_r($newArray);
I have array of students which is fetched in a variable $arraytosort and looks like
array(5027) { [0]=> array(5) { ["_id"]=> object(MongoId)#22 (1) { ["$id"]=> string(24) "58131c7799fbad4c1d000202" } ["fullname"]=> string(19) "David Robert Lara" ["firstmiddle"]=> string(12) "David Robert" ["firstlast"]=> string(14) "David Lara" ["student"]=> array(50) { ["_id"]=> object(MongoId)#23 (1) { ["$id"]=> string(24) "58131c7799fbad4c1d000202" } ["student_id"]=> float(2) ["registration_temp_perm_no"]=> string(1) "1" ["roll_no"]=> float(1) ["admission_date"]=> string(10) "01/07/2016" ["first_name"]=> string(7) "David" ["middle_name"]=> string(4) "Robert" ["last_name"]=> string(6) "Lara" ["dob"]=> string(10) "12/03/1981" ["gender"]=> string(6) "Female" ["blood_group"]=> string(2) "A+" ["birth_place"]=> string(11) "Sadar Bazar" ["nationality"]=> string(6) "Indian" ["language"]=> string(7) "English" ["religion"]=> string(8) "Agnostic" ["address_line1"]=> string(20) "4148 Hazelcrest Hill" ["address_line2"]=> string(20) "22883 Memorial Place" ["city"]=> string(11) "Sadar Bazar" ["state"]=> string(13) "Uttar Pradesh" ["pincode"]=> string(6) "190010" ["country"]=> string(5) "India" ["phone1"]=> string(10) "9039180419" ["phone2"]=> string(10) "7681559402" ["email"]=> string(24) "educianstudent#gmail.com" ["is_sms_enabled"]=> string(3) "Yes" ["is_active"]=> int(1) ["has_finished"]=> int(0) ["student_category"]=> string(1) "5" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["Biometric_ID"]=> string(1) "1" ["siblings"]=> string(14) "Cynthia Taylor" ["guardian_name"]=> string(14) "Cynthia Taylor" ["guardian_occupation"]=> string(13) "Senior Editor" ["guardian_qualification"]=> string(20) "Research Assistant I" ["guardian_email_id"]=> string(23) "educianparent#gmail.com" ["gaurdain_contact_details"]=> string(10) "9419513603" ["guardian_relationship"]=> string(6) "Father" ["height"]=> string(3) "4.9" ["weight"]=> string(4) "34.9" ["allergies"]=> string(0) "" ["batch"]=> int(2) ["academicyear"]=> string(4) "2015" ["batchhistory"]=> array(1) { [0]=> array(5) { ["batchid"]=> float(2) ["academic_year"]=> string(4) "2015" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["sequenceno"]=> int(1) ["courseId"]=> object(MongoId)#24 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } } } ["uploads"]=> array(1) { ["profile_pic"]=> string(39) "58131c7799fbad4c1d000202schoolgirl2.jpg" } ["created_at"]=> NULL ["updated_at"]=> string(0) "" ["routearray"]=> array(2) { [0]=> array(5) { ["routeid"]=> int(2) ["academicyear"]=> string(4) "2016" ["current"]=> int(0) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "09/28/2016" } [1]=> array(5) { ["routeid"]=> int(3) ["academicyear"]=> string(4) "2016" ["current"]=> int(1) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "11/17/2016" } } ["HostelAlloted"]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#25 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#26 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } ["HostelAllotmentHistory"]=> array(1) { [0]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#27 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#28 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } } ["courseId"]=> object(MongoId)#29 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } } }
...
...
}
I have written below function to sort these students on the basis of key value given in $key so that the array will contain the similar results on top/first.
$sortedarray = array();
$sortedarray = $this->multiSort(
$arraytosort,
$key,
'fullname',
'firstmiddle',
'firstlast',
'first_name',
'middle_name',
'last_name',
'roll_no',
'email'
);
function multiSort()
{
$args = func_get_args();
$c = count($args);
if ($c < 2) {
return false;
}
$key= $args[1];
$array = array_splice($args, 0, 1);
$array = $array[0];
usort($array, function($a) use($args) {
$i = 0;
$c = count($args);
$cmp = 0;
while($cmp == 0 && $i < $c)
{
$cmp = stripos($a[$args[$i] ],$key);
$i++;
}
return $cmp;
});
return $array;
}
It throws error message "Undefined variable: key".
Please help!!!
Hi I have this huge array:
array(14) {
["imagen-profesor"]=>
array(8) {
["id"]=>
string(15) "imagen-profesor"
["slug"]=>
string(15) "imagen-profesor"
["type"]=>
string(5) "image"
["name"]=>
string(15) "Imagen profesor"
["description"]=>
string(17) "Foto del profesor"
["data"]=>
array(8) {
["slug-pre-save"]=>
string(15) "imagen-profesor"
["placeholder"]=>
string(0) ""
["user_default_value"]=>
string(0) ""
["repetitive"]=>
string(1) "0"
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
["validate"]=>
array(1) {
["url2"]=>
array(3) {
["active"]=>
string(1) "1"
["message"]=>
string(60) "Please enter a valid URL address pointing to the image file."
["suppress_for_cred"]=>
bool(true)
}
}
}
["meta_key"]=>
string(20) "wpcf-imagen-profesor"
["meta_type"]=>
string(8) "usermeta"
}
["grados-academicos"]=>
array(8) {
["id"]=>
string(17) "grados-academicos"
["slug"]=>
string(17) "grados-academicos"
["type"]=>
string(7) "wysiwyg"
["name"]=>
string(18) "Grados académicos"
["description"]=>
string(0) ""
["data"]=>
array(5) {
["slug-pre-save"]=>
string(17) "grados-academicos"
["user_default_value"]=>
string(0) ""
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(22) "wpcf-grados-academicos"
["meta_type"]=>
string(8) "usermeta"
}
["experiencia-profesional"]=>
array(8) {
["id"]=>
string(23) "experiencia-profesional"
["slug"]=>
string(23) "experiencia-profesional"
["type"]=>
string(7) "wysiwyg"
["name"]=>
string(23) "Experiencia profesional"
["description"]=>
string(46) "Por favor coloca aquí experiencia profesional"
["data"]=>
array(5) {
["slug-pre-save"]=>
string(23) "experiencia-profesional"
["user_default_value"]=>
string(0) ""
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(28) "wpcf-experiencia-profesional"
["meta_type"]=>
string(8) "usermeta"
}
["areas-de-interes"]=>
array(8) {
["id"]=>
string(16) "areas-de-interes"
["slug"]=>
string(16) "areas-de-interes"
["type"]=>
string(7) "wysiwyg"
["name"]=>
string(18) "Áreas de interés"
["description"]=>
string(22) "Intereses del profesor"
["data"]=>
array(5) {
["slug-pre-save"]=>
string(16) "areas-de-interes"
["user_default_value"]=>
string(0) ""
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(21) "wpcf-areas-de-interes"
["meta_type"]=>
string(8) "usermeta"
}
["cursos-relevantes"]=>
array(8) {
["id"]=>
string(17) "cursos-relevantes"
["slug"]=>
string(17) "cursos-relevantes"
["type"]=>
string(7) "wysiwyg"
["name"]=>
string(17) "Cursos relevantes"
["description"]=>
string(7) "Cursos "
["data"]=>
array(5) {
["slug-pre-save"]=>
string(17) "cursos-relevantes"
["user_default_value"]=>
string(0) ""
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(22) "wpcf-cursos-relevantes"
["meta_type"]=>
string(8) "usermeta"
}
["articulos"]=>
array(8) {
["id"]=>
string(9) "articulos"
["slug"]=>
string(9) "articulos"
["type"]=>
string(7) "wysiwyg"
["name"]=>
string(10) "Artículos"
["description"]=>
string(0) ""
["data"]=>
array(5) {
["slug-pre-save"]=>
string(9) "articulos"
["user_default_value"]=>
string(0) ""
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(14) "wpcf-articulos"
["meta_type"]=>
string(8) "usermeta"
}
["areas-academicas"]=>
array(8) {
["id"]=>
string(16) "areas-academicas"
["slug"]=>
string(16) "areas-academicas"
["type"]=>
string(6) "select"
["name"]=>
string(18) "Áreas académicas"
["description"]=>
string(0) ""
["data"]=>
array(6) {
["slug-pre-save"]=>
string(16) "areas-academicas"
["options"]=>
array(12) {
["wpcf-fields-select-option-96129381b0cc289dd28e00d98f8c15b0-1"]=>
array(2) {
["title"]=>
string(23) "Análisis de decisiones"
["value"]=>
string(1) "1"
}
["wpcf-fields-select-option-f320b41329b42d462bd08eb7961f2c65-1"]=>
array(2) {
["title"]=>
string(17) "Comercialización"
["value"]=>
string(1) "2"
}
["wpcf-fields-select-option-f6b63eb3926b7de2462225998e09cc13-1"]=>
array(2) {
["title"]=>
string(32) "Control e información directiva"
["value"]=>
string(1) "3"
}
["wpcf-fields-select-option-b2610684e6f824fd687a74d3be7bcabe-1"]=>
array(2) {
["title"]=>
string(21) "Dirección financiera"
["value"]=>
string(1) "4"
}
["wpcf-fields-select-option-d2c3f7b635105db769c4772a15d5cec2-1"]=>
array(2) {
["title"]=>
string(25) "Dirección de operaciones"
["value"]=>
string(1) "5"
}
["wpcf-fields-select-option-f14bad56bc815fa8060ae231ccb8a217-1"]=>
array(2) {
["title"]=>
string(22) "Dirección de personal"
["value"]=>
string(1) "6"
}
["wpcf-fields-select-option-c6114cc9883ef2a4c8d23d9985fd41fc-1"]=>
array(2) {
["title"]=>
string(15) "Empresa-familia"
["value"]=>
string(1) "7"
}
["wpcf-fields-select-option-6378fcaa75be017d5cddec1f4c837bba-1"]=>
array(2) {
["title"]=>
string(18) "Entorno económico"
["value"]=>
string(1) "8"
}
["wpcf-fields-select-option-48713712cd807bd0920369faaea72630-1"]=>
array(2) {
["title"]=>
string(26) "Entorno político y social"
["value"]=>
string(1) "9"
}
["wpcf-fields-select-option-35c50a5e264b2951ccbd9a83f0d52185-1"]=>
array(2) {
["title"]=>
string(13) "Factor humano"
["value"]=>
string(2) "10"
}
["wpcf-fields-select-option-bdac1a39ab699e74fbffd3b6a9aa85be-1"]=>
array(2) {
["title"]=>
string(20) "Política de empresa"
["value"]=>
string(2) "11"
}
["default"]=>
string(10) "no-default"
}
["validate"]=>
array(1) {
["required"]=>
array(3) {
["active"]=>
string(1) "1"
["value"]=>
string(4) "true"
["message"]=>
string(25) "Por favor, elige el área"
}
}
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(21) "wpcf-areas-academicas"
["meta_type"]=>
string(8) "usermeta"
}
["calendario"]=>
array(8) {
["id"]=>
string(10) "calendario"
["slug"]=>
string(10) "calendario"
["type"]=>
string(4) "date"
["name"]=>
string(10) "Calendario"
["description"]=>
string(0) ""
["data"]=>
array(7) {
["slug-pre-save"]=>
string(10) "calendario"
["placeholder"]=>
string(0) ""
["date_and_time"]=>
string(4) "date"
["repetitive"]=>
string(1) "0"
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(15) "wpcf-calendario"
["meta_type"]=>
string(8) "usermeta"
}
["duracion"]=>
array(8) {
["id"]=>
string(8) "duracion"
["slug"]=>
string(8) "duracion"
["type"]=>
string(8) "textarea"
["name"]=>
string(9) "Duración"
["description"]=>
string(0) ""
["data"]=>
array(7) {
["slug-pre-save"]=>
string(8) "duracion"
["placeholder"]=>
string(0) ""
["user_default_value"]=>
string(0) ""
["repetitive"]=>
string(1) "0"
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(13) "wpcf-duracion"
["meta_type"]=>
string(8) "usermeta"
}
["fechas"]=>
array(8) {
["id"]=>
string(6) "fechas"
["slug"]=>
string(6) "fechas"
["type"]=>
string(8) "textarea"
["name"]=>
string(6) "Fechas"
["description"]=>
string(0) ""
["data"]=>
array(7) {
["slug-pre-save"]=>
string(6) "fechas"
["placeholder"]=>
string(0) ""
["user_default_value"]=>
string(0) ""
["repetitive"]=>
string(1) "0"
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(11) "wpcf-fechas"
["meta_type"]=>
string(8) "usermeta"
}
["contacto"]=>
array(8) {
["id"]=>
string(8) "contacto"
["slug"]=>
string(8) "contacto"
["type"]=>
string(7) "wysiwyg"
["name"]=>
string(8) "Contacto"
["description"]=>
string(0) ""
["data"]=>
array(5) {
["slug-pre-save"]=>
string(8) "contacto"
["user_default_value"]=>
string(0) ""
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(13) "wpcf-contacto"
["meta_type"]=>
string(8) "usermeta"
}
["areas-academicas-2"]=>
array(8) {
["id"]=>
string(18) "areas-academicas-2"
["slug"]=>
string(18) "areas-academicas-2"
["type"]=>
string(6) "select"
["name"]=>
string(24) "Segunda área académica"
["description"]=>
string(0) ""
["data"]=>
array(5) {
["slug-pre-save"]=>
string(18) "areas-academicas-2"
["options"]=>
array(12) {
["wpcf-fields-select-option-a84588db3f03c2837345f94adfa18588-1"]=>
array(2) {
["title"]=>
string(23) "Análisis de decisiones"
["value"]=>
string(1) "1"
}
["wpcf-fields-select-option-5a20ef6e60591caaf5bec848cf43cec0-1"]=>
array(2) {
["title"]=>
string(17) "Comercialización"
["value"]=>
string(1) "2"
}
["wpcf-fields-select-option-3b00b7d1d1040c63c3512a7e52446443-1"]=>
array(2) {
["title"]=>
string(32) "Control e información directiva"
["value"]=>
string(1) "3"
}
["wpcf-fields-select-option-da90a64b9b2bf7becfbec27c9c4025da-1"]=>
array(2) {
["title"]=>
string(21) "Dirección financiera"
["value"]=>
string(1) "4"
}
["wpcf-fields-select-option-65c40a4ea4cde81d6fb7b5bbe1c73282-1"]=>
array(2) {
["title"]=>
string(25) "Dirección de operaciones"
["value"]=>
string(1) "5"
}
["wpcf-fields-select-option-3b7a56cdb15cef8028daf43082269339-1"]=>
array(2) {
["title"]=>
string(22) "Dirección de personal"
["value"]=>
string(1) "6"
}
["wpcf-fields-select-option-6eded9521556a27a4cf4465c82f0a203-1"]=>
array(2) {
["title"]=>
string(15) "Empresa-familia"
["value"]=>
string(1) "7"
}
["wpcf-fields-select-option-5102092609e13386bfadc0ed367591b7-1"]=>
array(2) {
["title"]=>
string(18) "Entorno económico"
["value"]=>
string(1) "8"
}
["wpcf-fields-select-option-e1aadcfe82cc3389a57288a85ed45ff6-1"]=>
array(2) {
["title"]=>
string(26) "Entorno político y social"
["value"]=>
string(1) "9"
}
["wpcf-fields-select-option-6440b6d9a60739b88dacaafae2bccaa9-1"]=>
array(2) {
["title"]=>
string(13) "Factor humano"
["value"]=>
string(2) "10"
}
["wpcf-fields-select-option-2d081231584de301011755851f93b246-1"]=>
array(2) {
["title"]=>
string(20) "Política de empresa"
["value"]=>
string(2) "11"
}
["default"]=>
string(10) "no-default"
}
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(23) "wpcf-areas-academicas-2"
["meta_type"]=>
string(8) "usermeta"
}
["cargo-profesor"]=>
array(8) {
["id"]=>
string(14) "cargo-profesor"
["slug"]=>
string(14) "cargo-profesor"
["type"]=>
string(6) "select"
["name"]=>
string(14) "Cargo Profesor"
["description"]=>
string(36) "Selecciona un cargo para el profesor"
["data"]=>
array(6) {
["slug-pre-save"]=>
string(14) "cargo-profesor"
["options"]=>
array(5) {
["wpcf-fields-select-option-2a9da69d4d39817ac2a4b687ac49dc27-1"]=>
array(2) {
["title"]=>
string(18) "Director del área"
["value"]=>
string(1) "4"
}
["wpcf-fields-select-option-b7ae4c59b346a60a6680338c9c44c456-1"]=>
array(2) {
["title"]=>
string(8) "Profesor"
["value"]=>
string(1) "1"
}
["wpcf-fields-select-option-51a473bf339e2669511accaa90983fb0-1"]=>
array(2) {
["title"]=>
string(17) "Profesor invitado"
["value"]=>
string(1) "2"
}
["wpcf-fields-select-option-734e5707d3253ded0bf0d972f1c56afe-1"]=>
array(2) {
["title"]=>
string(6) "Decano"
["value"]=>
string(1) "3"
}
["default"]=>
string(10) "no-default"
}
["validate"]=>
array(1) {
["required"]=>
array(3) {
["active"]=>
string(1) "1"
["value"]=>
string(4) "true"
["message"]=>
string(23) "This field is required."
}
}
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(19) "wpcf-cargo-profesor"
["meta_type"]=>
string(8) "usermeta"
}
["segundo-cargo"]=>
array(8) {
["id"]=>
string(13) "segundo-cargo"
["slug"]=>
string(13) "segundo-cargo"
["type"]=>
string(6) "select"
["name"]=>
string(13) "Segundo cargo"
["description"]=>
string(0) ""
["data"]=>
array(5) {
["slug-pre-save"]=>
string(13) "segundo-cargo"
["options"]=>
array(5) {
["wpcf-fields-select-option-30ef69a1ba17b84f26edcee1404746f4-1"]=>
array(2) {
["title"]=>
string(18) "Director del área"
["value"]=>
string(1) "1"
}
["wpcf-fields-select-option-8b8dbd4c42d2e4d61053f442e82a88be-1"]=>
array(2) {
["title"]=>
string(8) "Profesor"
["value"]=>
string(1) "2"
}
["wpcf-fields-select-option-862f1fbf0e317265cf544b754d6fa3f0-1"]=>
array(2) {
["title"]=>
string(17) "Profesor invitado"
["value"]=>
string(1) "3"
}
["wpcf-fields-select-option-d49829cdb8f8aaae8af93f30f220ca86-1"]=>
array(2) {
["title"]=>
string(6) "Decano"
["value"]=>
string(1) "4"
}
["default"]=>
string(10) "no-default"
}
["custom_use"]=>
string(0) ""
["conditional_display"]=>
array(0) {
}
["disabled_by_type"]=>
int(0)
}
["meta_key"]=>
string(18) "wpcf-segundo-cargo"
["meta_type"]=>
string(8) "usermeta"
}
}
I just want to get this value: Análisis de decisiones, Comercialización, Control e información directiva, Dirección financiera, "Dirección de operaciones, Dirección de personal, Empresa-familia, Entorno económico, Entorno político y social, Factor humano, Política de empresa.
I mean what is inside areas-academicas
I'm using nested foreach but nothing:
foreach ($un as $key => $value)
{
if ( $key === "areas-academicas" )
{
echo "Llave: " . $key . " => ";
//echo $value;
foreach ( $value as $key2 => $value2 )
{
echo "Llave2: " . $key2 . "<br />" . "valor2: " . $value2;
foreach ($value2 as $key3 => $value3)
{
echo "Llave3: " . $key3 . "<br />" . "valor3: " . $value3;
}
}
}
}
What could be an easy way to get those values? thanks.
If I don't messed up with your nesting this should work. If it doesn't just fix the path inside the foreach.
$data = array(...); // the array you posted
foreach ($data["areas-academicas"]["data"]["options"] as $area) {
echo $area["title"];
}
The bottom line is you can start the iteration from any level, you don't have to begin at the top necessarily.
Looking at the Array it appears that youre only interested in two of arrays at the top level:
$un["areas-academicas"]
$un["areas-academicas-2"]
Then you are looking for specific values, titles of the options of the data, in the nested structure underneath these two:
$un["areas-academicas"]['data']['options'][....]['title']
You can probably limit the iterations of your loop by using the uniform structure of these arrays. But I will assume you wish to loop through the top level array for keys containing "areas-academicas":
$titles = array();
foreach ($un as $key => $value){
if(strpos($key, 'areas-academicas') == false){ continue; }
foreach($value['data']['options'] as $val){
$titles[] = $val['title'];
}
}
This loop should collect all the option titles from the areas-academicas arrays but is reliant on the idea that the nested structure will be uniform for each instance of the areas-academicas key in the top level array. Hope this helps
After making the authentication in sabre from php and make an inquiry about a flight, the response is a JSON string that I can not decipher, someone aid me, as I can show this readable string in php for my project.
part of the code, thanks for your help
object(SharedContext)#3 (1) {
["results":"SharedContext":private]=>
array(7) {
["SECURITY"]=>``
NULL
["origin"]=>
string(3) "MTR"
["destination"]=>
string(3) "BOG"
["departureDate"]=>
string(10) "2016-06-10"
["LeadPriceCalendar"]=>
object(stdClass)#6 (5) {
["status"]=>
string(8) "Complete"
["type"]=>
string(11) "Application"
["errorCode"]=>
string(20) "WARN.RAF.APPLICATION"
["timeStamp"]=>
string(25) "2016-06-03T22:04:40+00:00"
["message"]=>
string(21) "No results were found"
}
["InstaFlight"]=>
NULL
["BargainFinderMax"]=>
object(stdClass)#4 (2) {
["OTA_AirLowFareSearchRS"]=>
object(stdClass)#10 (11) {
["PricedItinCount"]=>
int(6)
["BrandedOneWayItinCount"]=>
int(0)
["SimpleOneWayItinCount"]=>
int(0)
["DepartedItinCount"]=>
int(0)
["SoldOutItinCount"]=>
int(0)
["AvailableItinCount"]=>
int(0)
["Version"]=>
string(5) "1.8.6"
["Success"]=>
object(stdClass)#11 (0) {
}
["Warnings"]=>
object(stdClass)#12 (1) {
["Warning"]=>
array(4) {
[0]=>
object(stdClass)#13 (5) {
["Type"]=>
string(12) "WORKERTHREAD"
["ShortText"]=>
string(19) "1750772809123328107"
["Code"]=>
string(13) "TRANSACTIONID"
["MessageClass"]=>
string(1) "I"
["content"]=>
string(0) ""
}
[1]=>
object(stdClass)#14 (5) {
["Type"]=>
string(6) "SERVER"
["ShortText"]=>
string(5) "27037"
["Code"]=>
string(9) "TTFHLC702"
["MessageClass"]=>
string(1) "I"
["content"]=>
string(0) ""
}
[2]=>
object(stdClass)#15 (5) {
["Type"]=>
string(7) "DEFAULT"
["ShortText"]=>
string(5) "13330"
["Code"]=>
string(6) "RULEID"
["MessageClass"]=>
string(1) "I"
["content"]=>
string(0) ""
}
[3]=>
object(stdClass)#16 (5) {
["Type"]=>
string(3) "DRE"
["ShortText"]=>
string(5) "13291"
["Code"]=>
string(6) "RULEID"
["MessageClass"]=>
string(1) "I"
["content"]=>
string(0) ""
}
}
}
["PricedItineraries"]=>
object(stdClass)#17 (1) {
["PricedItinerary"]=>
array(6) {
[0]=>
object(stdClass)#18 (5) {
["SequenceNumber"]=>
int(1)
["AirItinerary"]=>
object(stdClass)#19 (2) {
["DirectionInd"]=>
string(6) "OneWay"
["OriginDestinationOptions"]=>
object(stdClass)#20 (1) {
["OriginDestinationOption"]=>
array(1) {
[0]=>
object(stdClass)#21 (2) {
["ElapsedTime"]=>
int(76)
["FlightSegment"]=>
array(1) {
[0]=>
object(stdClass)#22 (15) {
["DepartureDateTime"]=>
string(19) "2016-06-10T08:38:00"
["ArrivalDateTime"]=>
string(19) "2016-06-10T09:54:00"
["StopQuantity"]=>
int(0)
["FlightNumber"]=>
string(4) "3193"
["ResBookDesigCode"]=>
string(1) "Q"
["ElapsedTime"]=>
int(76)
["DepartureAirport"]=>
object(stdClass)#23 (2) {
["LocationCode"]=>
string(3) "MTR"
["content"]=>
string(0) ""
}
["ArrivalAirport"]=>
object(stdClass)#24 (3) {
["LocationCode"]=>
string(3) "BOG"
["TerminalID"]=>
string(1) "1"
["content"]=>
string(0) ""
}
["OperatingAirline"]=>
object(stdClass)#25 (4) {
["CompanyShortName"]=>
string(26) "LAN COLOMBIA AIRLINES S.A."
["Code"]=>
string(2) "LA"
["FlightNumber"]=>
string(4) "3193"
["content"]=>
string(0) ""
}
["Equipment"]=>
array(1) {
[0]=>
object(stdClass)#26 (2) {
["AirEquipType"]=>
string(3) "320"
["content"]=>
string(0) ""
}
}
["MarketingAirline"]=>
object(stdClass)#27 (2) {
["Code"]=>
string(2) "LA"
["content"]=>
string(0) ""
}
["MarriageGrp"]=>
string(1) "O"
["DepartureTimeZone"]=>
object(stdClass)#28 (1) {
["GMTOffset"]=>
float(-5)
}
["ArrivalTimeZone"]=>
object(stdClass)#29 (1) {
["GMTOffset"]=>
float(-5)
}
["TPA_Extensions"]=>
object(stdClass)#30 (1) {
["eTicket"]=>
object(stdClass)#31 (1) {
["Ind"]=>
bool(true)
}
}
}
}
}
}
}
}