PHP Multidimensional Array Value Replacement -


i have been working on project fun.

it retrieves json game server, decodes array, , saves array values sqlite db (for display / manipulation later). new programming in general, , have never touched php prior this.

my question: there better, more efficient way handle this?

basically, section of code, loops through large multidimensional array, , replaces values equal string. prior insertion db can have fields formatted more readable.

the problem in actual script have huge list of defined variables now, , 3 foreach loops combined 15 or if/else if/else statements.

$sr = "summoners rift"; $rs = "ranked solo"; $rt = "ranked team"; $nr = "normal";  foreach ($history['games'] &$typemode) {     if ($typemode['subtype'] == 'ranked_solo_5x5')     {         $typemode['gamemode'] = $sr;         $typemode['subtype'] = $rs;     }     elseif ($typemode['subtype'] == 'ranked_team_5x5')     {         $typemode['gamemode'] = $sr;         $typemode['subtype'] = $rt;     }     elseif ($typemode['subtype'] == 'normal')     {         $typemode['gamemode'] = $sr;         $typemode['subtype'] = $nr;     } } 

the problem in actual script have huge list of defined variables now, , 3 foreach loops combined 15 or if / elseif / else statements.

the best solution can recommend based on data showing create basic array of structures connected data have , use foreach loop assign values based on initial array structure:

// set structured array values. $array_values = array(); $array_values['ranked_solo_5x5']['gamemode'] = "summoners rift"; $array_values['ranked_solo_5x5']['subtype'] = "ranked solo"; $array_values['ranked_team_5x5']['gamemode'] = "summoners rift"; $array_values['ranked_team_5x5']['subtype'] = "ranked team"; $array_values['normal']['gamemode'] = "summoners rift"; $array_values['normal']['subtype'] = "normal";  // set structured array values based on sub type. foreach ($history['games'] &$typemode) {    $typemode['gamemode'] = $array_values[$typemode['subtype']]['gamemode'];    $typemode['subtype'] = $array_values[$typemode['subtype']]['subtype']; } 

that way $array_values has preset values in place begin with. , assignment happens via array key access of $typemode['subtype'] in foreach loop.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -