Get information from an array without loops - php

I want to get term_id values from this array without any loops or foreach.
I need it To be:
First Term id is :
Second Term id is :
I tried using echo $term[0]->term_id; but it is not working
Here is My var_dump result:
array(2) {
[0]=>
object(WP_Term)#4046 (10) {
["term_id"]=>
int(72)
["name"]=>
string(14) "samsung"
["slug"]=>
string(91) "samsung"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(72)
["taxonomy"]=>
string(11) "product_cat"
["description"]=>
string(0) ""
["parent"]=>
int(63)
["count"]=>
int(5)
["filter"]=>
string(3) "raw"
}
[1]=>
object(WP_Term)#4045 (10) {
["term_id"]=>
int(64)
["name"]=>
string(15) "onclikck"
["slug"]=>
string(8) "onclikck"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(64)
["taxonomy"]=>
string(11) "product_cat"
["description"]=>
string(92) "Description"
["parent"]=>
int(63)
["count"]=>
int(3)
["filter"]=>
string(3) "raw"
}
}

Try it like this:
$term[0]['term_id']

Related

double loop to extract information from an array

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;
}
}

Php get from array

I have small problem, I can't get info from an array.
This is outcome when I var_dump variable.
array(1) {
["server"]=> array(34) {
["dedicated"]=> string(1) "d"
["game_descr"]=> string(14) "Counter-Strike"
["game_dir"]=> string(7) "cstrike"
["gq_address"]=> string(13) "185.119.89.91"
["gq_dedicated"]=> string(1) "d"
["gq_gametype"]=> string(14) "Counter-Strike"
["gq_hostname"]=> string(26) "Assassin'S CSDM [de_dust2]"
["gq_joinlink"]=> string(36) "steam://connect/185.119.89.91:27031/"
["gq_mapname"]=> string(8) "de_dust2"
["gq_maxplayers"]=> int(32)
["gq_mod"]=> string(7) "cstrike"
["gq_name"]=> string(18) "Counter-Strike 1.6"
["gq_numplayers"]=> int(32)
["gq_online"]=> bool(true)
["gq_password"]=> int(0)
["gq_port_client"]=> int(27031)
["gq_port_query"]=> int(27031)
["gq_protocol"]=> string(6) "source"
["gq_transport"]=> string(3) "udp"
["gq_type"]=> string(4) "cs16"
["hostname"]=> string(26) "Assassin'S CSDM [de_dust2]"
["map"]=> string(8) "de_dust2"
["max_players"]=> int(32)
["num_bots"]=> int(0)
["num_players"]=> int(32)
["os"]=> string(1) "l"
["password"]=> int(0)
["players"]=> array(0) { }
["port"]=> int(27031)
["protocol"]=> int(48)
["secure"]=> int(1)
["steamappid"]=> int(10)
["teams"]=> array(0) { }
["version"]=> string(13) "1.1.2.7/Stdio"
}
}
How to get hostname for example?
I tried like $info['hostname'], $info[0]['hostname']; But nothing works, why?
array(1) { ... }
The array has one thing in it.
["server"]=> array(34) { ... }
… which is another array with 34 things in it.
You are ignoring the outer array. You need to get the inner array and then access the item you want from that.
$info['server']['hostname']

Multidimensional array - Print only specific key value

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);

Cake PHP how to add array to find fields

I'm using cakephp 2.x and I would like to get certain element as field from my model. the problem is with my structure. If I use find all then I recieve this:
array(9) {
[0]=> ...
[1]=>
array(7) {
["Person"]=>
array(18) {
["id"]=>
int(2)
["imie"]=>
string(6) "STEFAN"
["nazwisko"]=>
string(8) "ŚMIAŁY"
["tel"]=>
string(13) "+55 648989748"
["mob"]=>
string(13) "+87 489747489"
["email"]=>
string(25) "HSDJKAHSDJK#HSDFJASHF.CIJ"
["tytul_naukowy"]=>
string(7) "TEZ NIE"
["data_od"]=>
string(10) "2017-02-16"
["data_do"]=>
NULL
["object_id"]=>
int(-1)
["object_type_id"]=>
int(2)
["duty_id"]=>
int(28)
["created"]=>
string(22) "2017-02-16 13:22:32+01"
["modified"]=>
string(22) "2017-02-16 13:22:32+01"
["deleted"]=>
int(0)
["additional_email"]=>
string(17) "UDIHAS#HFSDKFH.OK"
["description"]=>
string(4) "OPIS"
["displayName"]=>
string(15) "STEFAN ŚMIAŁY"
}
["Duty"]=>
array(3) {
["id"]=>
int(28)
["nazwa"]=>
string(20) "KOORDYNATOR RECEPCJI"
["atrybut"]=>
string(1) "W"
}
["ObjectType"]=>
array(3) {
["id"]=>
int(2)
["nazwa"]=>
string(4) "Obks"
["model"]=>
string(3) "Obk"
}
["Cro"]=>
array(12) {
["id"]=>
NULL
["created"]=>
NULL
["nazwa"]=>
NULL
["tel"]=>
NULL
["fax"]=>
NULL
["email"]=>
NULL
["www"]=>
NULL
["modified"]=>
NULL
["deleted"]=>
NULL
["cro_type_id"]=>
NULL
["displayName"]=>
NULL
["objectTypeIdWithId"]=>
NULL
}
["Attachment"]=>
array(0) {
}
["Specialization"]=>
array(0) {
}
["Obk"]=>
array(1) {
[0]=>
array(13) {
["id"]=>
int(2)
["nazwa"]=>
string(10) "OBK Z ID 2"
["tel"]=>
string(13) "+48 123456890"
["fax"]=>
string(13) "+48 123456789"
["email"]=>
string(15) "EMAIL#EMAIL.COM"
["www"]=>
string(11) "WWW.WWW.WWW"
["modified"]=>
string(22) "2017-03-20 15:11:31+01"
["created"]=>
string(22) "2014-05-12 21:50:17+02"
["deleted"]=>
int(0)
["local_order"]=>
int(3)
["displayName"]=>
string(10) "OBK Z ID 2"
["objectTypeIdWithId"]=>
string(3) "2_2"
["PeopleObk"]=>
array(3) {
["id"]=>
int(1)
["person_id"]=>
int(2)
["obk_id"]=>
int(2)
}
}
}
}
[2]=>
array(7) {
["Person"]=>
array(18) {
["id"]=>
int(3)
["imie"]=>
string(5) "KAMIL"
["nazwisko"]=>
string(10) "STEFAŃSKI"
["tel"]=>
string(13) "+23 123123123"
["mob"]=>
string(13) "+21 412312312"
["email"]=>
string(16) "ASDASD#SDFDSF.LS"
["tytul_naukowy"]=>
string(3) "NIE"
["data_od"]=>
string(10) "2017-02-22"
["data_do"]=>
NULL
["object_id"]=>
int(-1)
["object_type_id"]=>
int(2)
["duty_id"]=>
int(26)
["created"]=>
string(22) "2017-02-22 16:14:32+01"
["modified"]=>
string(22) "2017-02-22 16:14:32+01"
["deleted"]=>
int(0)
["additional_email"]=>
string(22) "ADAM.2313#WEBIMPULS.PL"
["description"]=>
string(4) "TEST"
["displayName"]=>
string(16) "KAMIL STEFAŃSKI"
}
["Duty"]=>
array(3) {
["id"]=>
int(26)
["nazwa"]=>
string(41) "KOORDYNATOR OŚRODKÓW BADAŃ KLINICZNYCH"
["atrybut"]=>
string(1) "W"
}
["ObjectType"]=>
array(3) {
["id"]=>
int(2)
["nazwa"]=>
string(4) "Obks"
["model"]=>
string(3) "Obk"
}
["Cro"]=>
array(12) {
["id"]=>
NULL
["created"]=>
NULL
["nazwa"]=>
NULL
["tel"]=>
NULL
["fax"]=>
NULL
["email"]=>
NULL
["www"]=>
NULL
["modified"]=>
NULL
["deleted"]=>
NULL
["cro_type_id"]=>
NULL
["displayName"]=>
NULL
["objectTypeIdWithId"]=>
NULL
}
["Attachment"]=>
array(0) {
}
["Specialization"]=>
array(0) {
}
["Obk"]=>
array(1) {
[0]=>
array(13) {
["id"]=>
int(3)
["nazwa"]=>
string(10) "OBK Z ID 3"
["tel"]=>
string(13) "+48 123456890"
["fax"]=>
string(13) "+48 123456789"
["email"]=>
string(15) "EMAIL#EMAIL.COM"
["www"]=>
string(11) "WWW.WWW.WWW"
["modified"]=>
string(22) "2017-03-20 10:45:24+01"
["created"]=>
string(22) "2014-05-16 09:09:33+02"
["deleted"]=>
int(0)
["local_order"]=>
int(1)
["displayName"]=>
string(10) "OBK Z ID 3"
["objectTypeIdWithId"]=>
string(3) "2_3"
["PeopleObk"]=>
array(3) {
["id"]=>
int(2)
["person_id"]=>
int(3)
["obk_id"]=>
int(3)
}
}
}
}
And I want just to retrieve Person.id, Person.displayName and Obk.0.id. The two first are no problem, but wheneve I try to get Obk.0.id I'm getting error: SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM for Table Obk.
How can I get something like that? I want to set the value in codition later, but it block my work.
In CakePHP 2 you can specify the fields you want returned by a find() query using the fields option like:-
$this->Foo->find('all', [
'fields' => ['Foo.id', 'Foo.name']
]);
This also works with contained models that have a belongsTo or hasOne relationship:-
$this->Foo->find('all', [
'fields' => ['Foo.id', 'Foo.name', 'Bar.name'],
'contain' => ['Bar']
]);
However, if the contained model is a hasMany or HABTM relationship you need to specify the fields of that model on the contain:-
$this->Foo->find('all', [
'fields' => ['Foo.id', 'Foo.name'],
'contain' => [
'Bar' => ['fields' => 'Bar.name']
]
]);
This is because CakePHP will be performing multiple SQL queries, so the contain needs to have the parameters for the query being run for the relevant model.

getting data from array within an object [duplicate]

I'm new to PHP and am trying to access the value of "contactId" from the following:
object(stdClass)#2 (4) {
["contacts"]=> array(1) {
[0]=> object(stdClass)#3 (18) {
["email"]=> string(24) "joewilliams#icontact.com"
["firstName"]=> string(3) "Joe"
["lastName"]=> string(8) "Williams"
["prefix"]=> string(3) "Mr."
["suffix"]=> string(3) "Jr."
["fax"]=> string(0) ""
["phone"]=> string(10) "8668039462"
["street"]=> string(21) "2365 Meridian Parkway"
["street2"]=> string(0) ""
["city"]=> string(6) "Durham"
["state"]=> string(2) "NC"
["postalCode"]=> string(5) "27713"
["createDate"]=> string(19) "2013-03-30 21:22:28"
["status"]=> string(6) "normal"
["bounceCount"]=> NULL
["contactId"]=> string(8) "67295434"
}
}
["limit"]=> int(20)
["offset"]=> int(0)
["total"]=> int(1)
}
How can I retrieve the value of contactId?
Many thanks!
These are not arrays, but an object containing an array containing an object. PHP object access syntax is with ->
$variableName->contacts[0]->contactId

Categories