I got many duplicates with this function - php

I know something wrong with this function. It is supposed to return an array with 12 keys and values but it returns 60. The problem should be the foreach loop. Everytime the if loop is false, it passes a value to $result. Can anyone tell me how to make it right?
function getFinaluserElearningLesson($array1, $array2){
$result = array();
for($i = 0; $i<count($array1); $i++){
foreach($array2 as $lesson2){
if($array1[$i]['lesson'] == $lesson2['lesson_id'] && $array1[$i]['user_id'] == $lesson2['user_id']){
$result[] = array(
'first_name' => $array1[$i]['first_name'],
'last_name' => $array1[$i]['last_name'],
'date_of_birth' => $array1[$i]['date_of_birth'],
'user_id' => $array1[$i]['user_id'],
'client_id' => $array1[$i]['client_id'],
'lesson' => $array1[$i]['lesson'],
'lessonName' => $array1[$i]['lessonName'],
'completion' => 'Completed'
);
}else{
$result[] = array(
'first_name' => $array1[$i]['first_name'],
'last_name' => $array1[$i]['last_name'],
'date_of_birth' => $array1[$i]['date_of_birth'],
'user_id' => $array1[$i]['user_id'],
'client_id' => $array1[$i]['client_id'],
'lesson' => $array1[$i]['lesson'],
'lessonName' => $array1[$i]['lessonName'],
'completion' => 'Incomplete'
);
}
}
}
return $result;
}
The output of array1 and array2 is:
array(12) {
[0]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "222"
["lessonName"]=>
string(6) "Asthma"
}
[1]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(2) "69"
["lessonName"]=>
string(23) "Cardiac Catheterization"
}
[2]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "308"
["lessonName"]=>
string(38) "CyberKnife Radiosurgery For Brain AVMs"
}
[3]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(2) "50"
["lessonName"]=>
string(47) "Durable Power of Attorney for Health Care (DPA)"
}
[4]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(2) "91"
["lessonName"]=>
string(12) "Heart Attack"
}
[5]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(10) "Wilczewski"
["date_of_birth"]=>
string(10) "1989-05-27"
["user_id"]=>
string(7) "2602204"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "500"
["lessonName"]=>
string(33) "Abdominal Aortic Aneurysm Surgery"
}
[6]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(10) "Wilczewski"
["date_of_birth"]=>
string(10) "1989-05-27"
["user_id"]=>
string(7) "2602204"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(1) "1"
["lessonName"]=>
string(19) "Cholesterol Control"
}
[7]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(10) "Wilczewski"
["date_of_birth"]=>
string(10) "1989-05-27"
["user_id"]=>
string(7) "2602204"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(1) "5"
["lessonName"]=>
string(16) "Types of Doctors"
}
[8]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["date_of_birth"]=>
string(10) "1983-09-14"
["user_id"]=>
string(7) "2685058"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "500"
["lessonName"]=>
string(33) "Abdominal Aortic Aneurysm Surgery"
}
[9]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["date_of_birth"]=>
string(10) "1983-09-14"
["user_id"]=>
string(7) "2685058"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "116"
["lessonName"]=>
string(4) "Acne"
}
[10]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["date_of_birth"]=>
string(10) "1983-09-14"
["user_id"]=>
string(7) "2685058"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "122"
["lessonName"]=>
string(5) "Burns"
}
[11]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["date_of_birth"]=>
string(10) "1983-09-14"
["user_id"]=>
string(7) "2685058"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "546"
["lessonName"]=>
string(60) "Peripherally Inserted Central Catheter (PICC) for Outpatient"
}
}
array(5) {
[0]=>
array(2) {
["user_id"]=>
string(6) "951301"
["lesson_id"]=>
string(2) "35"
}
[1]=>
array(2) {
["user_id"]=>
string(7) "1700981"
["lesson_id"]=>
string(2) "35"
}
[2]=>
array(2) {
["user_id"]=>
string(7) "2585455"
["lesson_id"]=>
string(2) "25"
}
[3]=>
array(2) {
["user_id"]=>
string(7) "2602204"
["lesson_id"]=>
string(3) "500"
}
[4]=>
array(2) {
["user_id"]=>
string(7) "2642677"
["lesson_id"]=>
string(3) "180"
}
}

You could simplify the inner if() DRASTICALLY by doing this:
if($array1[$i]['lesson'] == $lesson2['lesson_id'] && $array1[$i]['user_id'] == $lesson2['user_id']){
$completion = 'Complete';
} else {
$completion = 'Incomplete';
}
$result[] = array(
'first_name' => $array1[$i]['first_name'],
'last_name' => $array1[$i]['last_name'],
'date_of_birth' => $array1[$i]['date_of_birth'],
'user_id' => $array1[$i]['user_id'],
'client_id' => $array1[$i]['client_id'],
'lesson' => $array1[$i]['lesson'],
'lessonName' => $array1[$i]['lessonName'],
'completion' => $completion // <---- the **ONLY** difference between the two versions
);
As far as your "duplication" problem... I'm not sure what you're trying to accomplish with this. You're running a nested loop, with 5 outer and 12 inner members, which gives your 60 array entries. Since you're assigning something to $result inside the inner loop, no matter WHAT the if() condition returns, that'll produce 60 assignments.
If you don't want the 'false' ones to happen, then don't do anything in the else clause of your code.

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: Convert Google Sheets result object to simple array

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.

Removing duplicate data in a multi dimension array (associative array)

I have three keys pointing to value of type array, in those array values I have duplicate values. How can I remove them?
array(3) {
["rock"]=>
array(4) {
[0]=>
array(9) {
["id"]=>
string(1) "1"
["title"]=>
string(15) "Teleman Tickets"
["location"]=>
string(20) "Concorde 2, Brighton"
["event_date"]=>
string(10) "2017-02-20"
["event_time"]=>
string(8) "20:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "50.817321799999990"
["geo_long"]=>
string(17) "-0.12304610000001"
["tags"]=>
string(30) "rock,alternative,indie"
}
[1]=>
array(9) {
["id"]=>
string(1) "4"
["title"]=>
string(9) "Blink-182"
["location"]=>
string(25) "Motorpoint Arena, Cardiff"
["event_date"]=>
string(10) "2017-07-03"
["event_time"]=>
string(8) "18:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "51.478937400000010"
["geo_long"]=>
string(17) "-3.17172289999996"
["tags"]=>
string(14) "rock,metal"
}
[2]=>
array(9) {
["id"]=>
string(1) "8"
["title"]=>
string(5) "Ghost"
["location"]=>
string(29) "O2 Forum Kentish Town, London"
["event_date"]=>
string(10) "2017-03-26"
["event_time"]=>
string(8) "19:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "51.552197000000010"
["geo_long"]=>
string(17) "-0.14196900000002"
["tags"]=>
string(30) "rock,alternative,indie"
}
[3]=>
array(9) {
["id"]=>
string(2) "10"
["title"]=>
string(11) "Courteeners"
["location"]=>
string(39) "Emirates Old Trafford, Lancashire C.C.C"
["event_date"]=>
string(10) "2017-05-27"
["event_time"]=>
string(8) "16:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "53.456428000000000"
["geo_long"]=>
string(17) "-2.28679699999998"
["tags"]=>
string(30) "rock,alternative,indie"
}
}
["alternative"]=>
array(4) {
[0]=>
array(9) {
["id"]=>
string(1) "1"
["title"]=>
string(15) "Teleman Tickets"
["location"]=>
string(20) "Concorde 2, Brighton"
["event_date"]=>
string(10) "2017-02-20"
["event_time"]=>
string(8) "20:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "50.817321799999990"
["geo_long"]=>
string(17) "-0.12304610000001"
["tags"]=>
string(30) "rock,alternative,indie"
}
[1]=>
array(9) {
["id"]=>
string(1) "6"
["title"]=>
string(6) "Sum 41"
["location"]=>
string(23) "O2 Academy Leeds, Leeds"
["event_date"]=>
string(10) "2017-02-26"
["event_time"]=>
string(8) "19:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "53.802188400000000"
["geo_long"]=>
string(17) "-1.54713770000001"
["tags"]=>
string(21) "alternative,indie"
}
[2]=>
array(9) {
["id"]=>
string(1) "8"
["title"]=>
string(5) "Ghost"
["location"]=>
string(29) "O2 Forum Kentish Town, London"
["event_date"]=>
string(10) "2017-03-26"
["event_time"]=>
string(8) "19:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "51.552197000000010"
["geo_long"]=>
string(17) "-0.14196900000002"
["tags"]=>
string(30) "rock,alternative,indie"
}
[3]=>
array(9) {
["id"]=>
string(2) "10"
["title"]=>
string(11) "Courteeners"
["location"]=>
string(39) "Emirates Old Trafford, Lancashire C.C.C"
["event_date"]=>
string(10) "2017-05-27"
["event_time"]=>
string(8) "16:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "53.456428000000000"
["geo_long"]=>
string(17) "-2.28679699999998"
["tags"]=>
string(30) "rock,alternative,indie"
}
}
["sax"]=>
array(1) {
[0]=>
array(9) {
["id"]=>
string(1) "3"
["title"]=>
string(9) "Take That"
["location"]=>
string(22) "The SSE Hydro, Glasgow"
["event_date"]=>
string(10) "2017-05-11"
["event_time"]=>
string(8) "18:30:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "55.860156000000000"
["geo_long"]=>
string(17) "-4.28525800000000"
["tags"]=>
string(24) "pop,boy-band,sax"
}
}
}
I'm going to assume that if two bands have the same 'id' then they are identical
$unique_bands = array();
// if the master array is called bands
foreach($bands as $genre){
foreach($genre as $band){
$unqiue_bands[$band['id']] = $band;
}
}
Maybe put $unique_bands = array_values(array_filter($unique_bands)); at the end to reindex the array
You have a solution on the official documentation... A person made a simple function to do exactly what you want.
Example array:
<?php
$details = array(
0 => array("id"=>"1", "name"=>"Mike", "num"=>"9876543210"),
1 => array("id"=>"2", "name"=>"Carissa", "num"=>"08548596258"),
2 => array("id"=>"1", "name"=>"Mathew", "num"=>"784581254"),
);
?>
Function:
<?php
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
?>
You will have to fix it to work your for array dimension.

using array_unique, I appear to have stripped out the records that were duplicated?

Within available_options I have somehow stripped out Express when I just wanted to keep one of them?
The array looks like this
["options"]=>
array(9) {
[0]=>
array(8) {
["id"]=>
string(2) "79"
["product_id"]=>
string(2) "15"
["sku"]=>
string(9) "CSR-FTC4S"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "35.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "219"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(19) "Five Ten C4 Stealth"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(5) "FTC4S"
["user_value"]=>
string(25) "Five Ten C4 Stealth 5.5mm"
["sequence"]=>
string(1) "0"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "220"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
[2]=>
array(6) {
["id"]=>
string(3) "221"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "23"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(16) "Rand Toe Patches"
["name"]=>
string(3) "RTP"
["sku"]=>
string(3) "RTP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "10.000"
}
}
}
}
}
[1]=>
array(8) {
["id"]=>
string(2) "80"
["product_id"]=>
string(2) "15"
["sku"]=>
string(10) "CSR-FTONYX"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "37.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "222"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "2"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "2"
["admin_name"]=>
string(13) "Five Ten Onyx"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(6) "FTONYX"
["user_value"]=>
string(19) "Five Ten Onyx 4.5mm"
["sequence"]=>
string(1) "1"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "223"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
and my code goes like this
foreach($this->_data as &$data) {
foreach($data['options'] as &$option) {
$option['available_options'] = array_unique($option['available_options']);
}
}
It's working apart from it's stripped out the duplicates rather than showing them once?
array_unique does not work recursively, you need to go inside your array to apply it on option_data directly.
foreach($this->_data as &$data) {
foreach ($data['options'] as &$option) {
foreach ($option['available_options'] as &$available_option) {
foreach ($available_option['option_data'] as &$option_data) {
$option_data = array_unique($option_data);
}
}
}
}
This way, the last option_data looks like
'option_data' => [
[
'id' => '1',
'admin_name' => 'Express',
'sku' => 'EXP',
'sequence' => '9999',
'option_price' => '25.000'
]
]
But as you can see, the value Express only appear once, but user_value and status are removed too, because there value is 1, like id.

Displaying an array of values via foreach loop and how can I limit the array to be displayed?

I want to know how can I limit the values of array to be displayed. Assuming my $color is equals to Indigo
I tried implementing this code by it doesn't work
foreach($data as $item){
if($item['color'] == $color){
echo $item['size'].'-'.$item['color'].'-'.$item['price'].'<br>';
}
}
And if I remove the if statement the var_dump($data)
array(16) {
[0]=>
array(3) {
["size"]=>
string(1) "2"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[1]=>
array(3) {
["size"]=>
string(1) "2"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[2]=>
array(3) {
["size"]=>
string(1) "4"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[3]=>
array(3) {
["size"]=>
string(1) "4"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[4]=>
array(3) {
["size"]=>
string(1) "6"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[5]=>
array(3) {
["size"]=>
string(1) "6"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[6]=>
array(3) {
["size"]=>
string(1) "8"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[7]=>
array(3) {
["size"]=>
string(1) "8"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[8]=>
array(3) {
["size"]=>
string(2) "10"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[9]=>
array(3) {
["size"]=>
string(2) "10"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[10]=>
array(3) {
["size"]=>
string(2) "12"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[11]=>
array(3) {
["size"]=>
string(2) "12"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[12]=>
array(3) {
["size"]=>
string(2) "14"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[13]=>
array(3) {
["size"]=>
string(2) "14"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[14]=>
array(3) {
["size"]=>
string(2) "16"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[15]=>
array(3) {
["size"]=>
string(2) "16"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
}
I want only to display values which is the color is equal to $color. How should I do this the correct way?
You might be dealing with case sensitivity or white space .. you can try :
$data = array(
0 => array("size" => "2","price" => "$59.00","color" => "Black Rinse"),
1 => array("size" => "2","price" => "$59.00","color" => "Indigo"));
$color = "Indigo";
foreach ( $data as $item ) {
if (strtolower($item['color']) == strtolower($color)) {
echo $item['size'] . '-' . $item['color'] . '-' . $item['price'] . '<br>';
}
}
Output
2-Indigo-$59.00
See Live Demo
This should fix your issue (assuming you're comparing two strings, I suggest you use strcasecmp- for case insensitive string comparison in your if loop)
foreach($data as $item){
if(strcasecmp(trim($item['color']),trim($color))==0){
echo $item['size'].'-'.$item['color'].'-'.$item['price'].'<br>';
}
}
Be elegant, be simple:
$items = array(
array('size'=>'4', 'color'=>'Indigo', 'price'=>'$59.00'),
array('size'=>'5', 'color'=>'black', 'price'=>'$59.00'),
array('size'=>'5', 'color'=>'green', 'price'=>'$54.00'),
array('size'=>'7', 'color'=>'Indigo', 'price'=>'$50.00'),
array('size'=>'5', 'color'=>'purple', 'price'=>'$51.00'));
print_r(array_filter($items, function($i){
return in_array('Indigo', $i);
})
);
or PHP < 5.3:
print_r(array_filter($items,
create_function('$i', 'return in_array("Indigo", $i);'))
);
Take advantage of features that PHP offers.

Categories