Multidimensional array - Print only specific key value - php

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

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

Sorting arrays and arrays of arrays using a key parameter

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!!!

Get information from an array without loops

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']

PHP Fatal error out of memory while building tree

I got trouble with bulding tree of categories in php.
It's builds tree for very long time and after that i got error:
Fatal error: Out of memory (allocated 637796352) (tried to allocate
208666926 bytes) in
D:\OpenServer\domains\site.local\Core\Widgets.php on line 488
Here is my functions to build tree.
First i made an array of categories:
public function makeCategoriesArray()
{
// $row = DB::select()
// ->from('catalog_tree')
// ->where('parent_id', '=', $this->post['parent_id'])
// ->union(DB::select()
// ->from('catalog_tree')
// ->where('id', 'in', DB::expr("(SELECT catalog_tree_id FROM catalog_tree_parents WHERE parent_id = '".$this->post['parent_id']."')")))
// ->find_all();
$row = DB::select()
->from('catalog_tree')
->where('status', '=', '1')
->find_all();
$categories_arr = array();
foreach ($row as $cat) {
$categories_arr[$cat->parent_id][] = $cat;
}
unset($cat);
unset($row);
foreach($categories_arr as $key=>$value) {
$row = DB::select()
->from('catalog_tree')
->where('id', 'in', DB::expr("(SELECT catalog_tree_id FROM catalog_tree_parents WHERE parent_id = '".$key."')"))
->find_all();
foreach($row as $cat) {
$categories_arr[$key][] = $cat;
}
unset($cat);
unset($row);
}
return $categories_arr;
}
Then i begin to build tree with this array:
public function BuildMainTree($array, $pid) {
global $MainMenu;
if (isset($array[$pid])) { // If category with $pid exist
$MainMenu.= "<ul class=\"dropdown-menu\" role=\"menu\">";
foreach ($array[$pid] as $value)
{
if ($pid!='0' && !isset($array[$value->id])) {
$MainMenu.= '<li class="divider"></li>
<li class="dropdown">
<a href="/catalog/' . $value->alias . '">
'. $value->name .'
</a>
</li>';
}
else {
$MainMenu.= '<li class="divider"></li>
<li class="dropdown">
<a href="/catalog/' . $value->alias . '">
'. $value->name .'
<span class="caret"></span>
</a>
</li>';
$this->BuildMainTree($array, $value->id);
}
}
if ($pid=='0') {
$MainMenu .= "</ul>";
}
else {
$MainMenu .= "</ul></li>";
}
}
}
Initialization of this process are in this function:
public function Header() {
$contentMenu = Common::factory('sitemenu')->getRows(1, 'sort');
$array['contentMenu'] = $contentMenu;
$array['user'] = User::info();
$array['countItemsInTheCart'] = Cart::factory()->_count_goods;
$categories_arr = $this->makeCategoriesArray();
$this->BuildMainTree($categories_arr, 0);
$array['mainMenu'] = $MainMenu;
return $array;
}
Also if i just make array with makeCategoriesArray() it's works fine, but BuildMainTree($array, $pid) function causes an error...
Most funny part, is that this code works perfect on other site.
Maybe I just missed some details here... Spent two days of tried to run this code, but got nothing.
Here is array var_dump:
array(156) {
[0]=>
array(21) {
[0]=>
object(stdClass)#17 (18) {
["id"]=>
string(4) "2075"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1473775697"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(27) "Садовая мебель"
["alias"]=>
string(14) "sadovaja-mebel"
["ikea_alias"]=>
string(7) "outdoor"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "dc30302d5e76ddf7d3be9cb0f8b4d711.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "129"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[1]=>
object(stdClass)#19 (18) {
["id"]=>
string(4) "2076"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1473950659"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(39) "Оформление интерьера"
["alias"]=>
string(19) "oformlenie-interera"
["ikea_alias"]=>
string(10) "decoration"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "91b3786d06e22081abbb945b9e4e02d8.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "287"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[2]=>
object(stdClass)#20 (18) {
["id"]=>
string(4) "2077"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472474523"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(27) "Офисная мебель"
["alias"]=>
string(14) "ofisnaja-mebel"
["ikea_alias"]=>
string(10) "workspaces"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "6884cb6cf5defdf8ec7fe467bad542ca.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "247"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[3]=>
object(stdClass)#21 (18) {
["id"]=>
string(4) "2078"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472409476"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(34) "Посуда для готовки"
["alias"]=>
string(19) "posuda-dlja-gotovki"
["ikea_alias"]=>
string(7) "cooking"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "ca3e4a8eb9d0d7ea22f9a7fc3c300ef9.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "514"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[4]=>
object(stdClass)#22 (18) {
["id"]=>
string(4) "2079"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472547042"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(16) "Столовая"
["alias"]=>
string(9) "stolovaja"
["ikea_alias"]=>
string(6) "dining"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "d5704ec3e70a3415b181c7c4f9005c1a.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "147"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[5]=>
object(stdClass)#23 (18) {
["id"]=>
string(4) "2080"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472547236"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(31) "Сервировка стола"
["alias"]=>
string(16) "servirovka-stola"
["ikea_alias"]=>
string(6) "eating"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "2d2142a8edd5fc3ce1ffa01fbe6655c9.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "338"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[6]=>
object(stdClass)#24 (18) {
["id"]=>
string(4) "2081"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472540142"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(21) "Кухни МЕТОД"
["alias"]=>
string(11) "kuhni-metod"
["ikea_alias"]=>
string(7) "kitchen"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "decf1d321c63aed673d949b34c452a37.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "304"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[7]=>
object(stdClass)#25 (18) {
["id"]=>
string(4) "2082"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472466401"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(18) "Освещение"
["alias"]=>
string(11) "osveschenie"
["ikea_alias"]=>
string(8) "lighting"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "3ac3d00a4734040cf346f2a63a09a7f9.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "243"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[8]=>
object(stdClass)#26 (18) {
["id"]=>
string(4) "2083"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472547518"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(14) "Детская"
["alias"]=>
string(8) "detskaja"
["ikea_alias"]=>
string(14) "childrens_ikea"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "8d78781b99b8ee7a1abc0f9cbfdc7e01.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "321"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[9]=>
object(stdClass)#27 (18) {
["id"]=>
string(4) "2084"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472411322"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(16) "Гостиная"
["alias"]=>
string(9) "gostinaja"
["ikea_alias"]=>
string(11) "living_room"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "c9c933ae12652bca96eb2ef66c832a82.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "181"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[10]=>
object(stdClass)#28 (18) {
["id"]=>
string(4) "2085"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472388754"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(35) "Домашняя прачечная"
["alias"]=>
string(24) "domashnjaja-prachechnaja"
["ikea_alias"]=>
string(7) "laundry"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "08c9713d828235679b861c095784af0e.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "106"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[11]=>
object(stdClass)#29 (18) {
["id"]=>
string(4) "2086"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472481068"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(45) "Дополнительное хранение"
["alias"]=>
string(22) "dopolnitelnoe-hranenie"
["ikea_alias"]=>
string(17) "secondary_storage"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "bb180b85cc1e34ab9b6185f4cf71fbf0.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "158"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[12]=>
object(stdClass)#31 (18) {
["id"]=>
string(4) "2088"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472136224"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(16) "Прихожая"
["alias"]=>
string(10) "prihozhaja"
["ikea_alias"]=>
string(7) "hallway"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "985600d075b4080f57488b87e0e898ff.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(2) "64"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[13]=>
object(stdClass)#32 (18) {
["id"]=>
string(4) "2089"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472556911"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(14) "Спальня"
["alias"]=>
string(7) "spalnja"
["ikea_alias"]=>
string(7) "bedroom"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "439a1f90391568bffabcfbc5a8b22559.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "172"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[14]=>
object(stdClass)#33 (18) {
["id"]=>
string(4) "2090"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472481070"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(18) "Еда в ИКЕА"
["alias"]=>
string(10) "eda-v-ikea"
["ikea_alias"]=>
string(4) "food"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "1886868c925ed2f22a10fe7e51a6050c.JPG"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(2) "24"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[15]=>
object(stdClass)#34 (18) {
["id"]=>
string(4) "2091"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472554267"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(16) "Текстиль"
["alias"]=>
string(7) "tekstil"
["ikea_alias"]=>
string(8) "Textiles"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "1ed5b13bc1abab2d5966b6ab074b7174.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "194"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[16]=>
object(stdClass)#35 (18) {
["id"]=>
string(4) "2092"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1473774214"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(33) "Домашние растения"
["alias"]=>
string(19) "domashnie-rastenija"
["ikea_alias"]=>
string(16) "indoor_gardening"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "a42bf2728494d635bc16b0f12db5855d.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
string(0) ""
["views"]=>
string(2) "55"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[17]=>
object(stdClass)#36 (18) {
["id"]=>
string(4) "2093"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472481066"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(39) "Беспроводная зарядка"
["alias"]=>
string(22) "besprovodnaja-zarjadka"
["ikea_alias"]=>
string(17) "wireless_charging"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "6ee6de2d3769d7f617b7e05fd81fbaa4.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(2) "95"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[18]=>
object(stdClass)#37 (18) {
["id"]=>
string(4) "2094"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472517463"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(12) "Ванная"
["alias"]=>
string(7) "vannaja"
["ikea_alias"]=>
string(8) "bathroom"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "f69159cedca764bc7d44ff7d6a13b52a.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "157"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[19]=>
object(stdClass)#38 (18) {
["id"]=>
string(4) "2095"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472379447"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(24) "Товары IKEA FAMILY"
["alias"]=>
string(18) "tovary-ikea-family"
["ikea_alias"]=>
string(20) "ikea_family_products"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "9e0c8d67fd4189ebed7b821e1c6b9df8.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(2) "78"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
[20]=>
object(stdClass)#39 (18) {
["id"]=>
string(4) "2096"
["created_at"]=>
string(10) "1470080793"
["updated_at"]=>
string(10) "1472482584"
["status"]=>
string(1) "1"
["sort"]=>
string(1) "0"
["name"]=>
string(34) "Для вашего бизнеса"
["alias"]=>
string(20) "dlja-vashego-biznesa"
["ikea_alias"]=>
string(8) "business"
["parent_id"]=>
string(1) "0"
["image"]=>
string(36) "3b9d8f1d77703f38d3d043fb95dd2b5e.jpg"
["h1"]=>
NULL
["title"]=>
NULL
["keywords"]=>
NULL
["description"]=>
NULL
["text"]=>
NULL
["views"]=>
string(3) "118"
["course"]=>
string(4) "9.50"
["discount_percent"]=>
string(1) "0"
}
}
that is not a code issue. set memory_limit in php.ini file to more than 128m,
you should also increase post meta data to something like 10000
Problem solved with this function:
public static function BuildTree($pid) {
global $MainMenu;
$row = DB::select()
->from('catalog_tree')
->where('parent_id', '=', $pid)
->and_where('status', '=', '1')
->union(DB::select()
->from('catalog_tree')
->where('id', 'in', DB::expr("(SELECT catalog_tree_id FROM catalog_tree_parents WHERE parent_id = '".$pid."')"))
->and_where('status', '=', '1'))
->find_all();
if ($row->count() > 0) {
$MainMenu .= "<ul class=\"dropdown-menu\" role=\"menu\">";
foreach ($row as $value)
{
$MainMenu .= '
<li class="divider"></li>
<li class="dropdown">
<a href="/catalog/' . $value->alias . '">
'. $value->name .'
</a> ';
Widgets::BuildTree($value->id);
}
if ($pid=='0') {
$MainMenu .= " <li class=\"divider\"></li></ul>";
return $MainMenu;
}
else {
$MainMenu .= " <li class=\"divider\"></li></ul></li>";
}
}
}
This function works perfectly :)
I think #DoktorOSwaldo was right, previous recursion was endless.

Select the value from an associative massive array

I am trying to get some value from the massive array, and there occured some problem.
I have such array:
array(2) {
[0]=>
array(23) {
["ITEM_ID"]=>
int(2331)
["ITEM_TYPE_ID"]=>
string(1) "1"
["CART_ITEM_ID"]=>
string(0) ""
["SAVED_TYPE_ID"]=>
string(0) ""
["ITEM_NAME"]=>
string(17) "TEST STOCK LEVELS"
["ERROR"]=>
string(0) ""
["PROPERTIES"]=>
array(1) {
[522]=>
array(1) {
[0]=>
string(4) "1122"
}
}
["PROPERTIES_INFO"]=>
array(1) {
[522]=>
array(12) {
["USAGE_TYPE"]=>
string(1) "1"
["CONTROL"]=>
string(7) "LISTBOX"
["TYPE"]=>
string(1) "1"
["NAME"]=>
string(11) "TEST OPTION"
["VALUES"]=>
array(1) {
[0]=>
string(4) "1122"
}
["REQUIRED"]=>
string(1) "1"
["PARENT_PROPERTY_ID"]=>
string(0) ""
["PARENT_VALUE_ID"]=>
string(0) ""
["TEXT"]=>
array(0) {
}
["CONTROL_PRICE"]=>
int(0)
["ORDER"]=>
string(1) "1"
["QUANTITY_ACTION"]=>
string(1) "1"
}
}
["PROPERTIES_PRICE"]=>
float(10)
["PROPERTIES_PERCENTAGE"]=>
int(0)
["PROPERTIES_BUYING"]=>
int(0)
["PROPERTIES_DISCOUNT"]=>
int(0)
["PROPERTIES_MORE"]=>
string(1) "0"
["COMPONENTS"]=>
array(0) {
}
["QUANTITY"]=>
int(11)
["TAX_FREE"]=>
string(1) "0"
["DISCOUNT"]=>
int(1)
["BUYING_PRICE"]=>
string(0) ""
["PRICE_EDIT"]=>
string(1) "0"
["PRICE"]=>
string(5) "10.00"
["CHECK_PRICE"]=>
float(20)
["MAX_ITEM_DISCOUNT"]=>
float(20)
["CART_PRICE"]=>
float(20)
}
[1]=>
array(23) {
["ITEM_ID"]=>
int(2331)
["ITEM_TYPE_ID"]=>
string(1) "1"
["CART_ITEM_ID"]=>
string(0) ""
["SAVED_TYPE_ID"]=>
string(0) ""
["ITEM_NAME"]=>
string(17) "TEST STOCK LEVELS"
["ERROR"]=>
string(0) ""
["PROPERTIES"]=>
array(1) {
[522]=>
array(1) {
[0]=>
string(4) "1123"
}
}
["PROPERTIES_INFO"]=>
array(1) {
[522]=>
array(12) {
["USAGE_TYPE"]=>
string(1) "1"
["CONTROL"]=>
string(7) "LISTBOX"
["TYPE"]=>
string(1) "1"
["NAME"]=>
string(11) "TEST OPTION"
["VALUES"]=>
array(1) {
[0]=>
string(4) "1123"
}
["REQUIRED"]=>
string(1) "1"
["PARENT_PROPERTY_ID"]=>
string(0) ""
["PARENT_VALUE_ID"]=>
string(0) ""
["TEXT"]=>
array(0) {
}
["CONTROL_PRICE"]=>
int(0)
["ORDER"]=>
string(1) "1"
["QUANTITY_ACTION"]=>
string(1) "1"
}
}
["PROPERTIES_PRICE"]=>
float(20)
["PROPERTIES_PERCENTAGE"]=>
int(0)
["PROPERTIES_BUYING"]=>
int(0)
["PROPERTIES_DISCOUNT"]=>
int(0)
["PROPERTIES_MORE"]=>
string(1) "0"
["COMPONENTS"]=>
array(0) {
}
["QUANTITY"]=>
int(5)
["TAX_FREE"]=>
string(1) "0"
["DISCOUNT"]=>
int(1)
["BUYING_PRICE"]=>
string(0) ""
["PRICE_EDIT"]=>
string(1) "0"
["PRICE"]=>
string(5) "10.00"
["CHECK_PRICE"]=>
float(30)
["MAX_ITEM_DISCOUNT"]=>
float(30)
["CART_PRICE"]=>
float(30)
}
}
Before looping this array, I receive some property_id (1123 for example).
I need to check if this property_id matches this element (1122 in my case)
["PROPERTIES"]=>
array(1) {
[522]=>
array(1) {
[0]=>
string(4) "1122"
}
}
then select its quantity property (in my case it is this element)
["QUANTITY"]=>
int(11)
The problem is to identify from which arrays select this property, because first I need to compare my received value with existing in array(1122 in my case).
I think you could do it faster with a better organized array. As it is I'll do a loop.
Something like that :
$quantity = 0;
$which_array = -1;
foreach($massive as $key=>$value){
if(isset($value['PROPERTIES_INFO'])){
$copy_properties = $value['PROPERTIES_INFO'];
$property_first = reset($copy_properties);
$id_property = key($property_first);
if($id_property == $what_id_you_are_looking_for){
$quantity = $value['QUANTITY'];//delete one of this
//or I don't know wich quantity you want so I put them both
$quantity = $value['PROPERTIES_INFO'][$id_property ]['QUANTITY_ACTION'];//delete one of this
$which_array = $key;
break;
}
}
}

Categories