php - Replacement of array values -
newbie here. i'm trying create wordpress plugin, , following code display dropdown font selection. works looks wrong , not efficient, way change array's value display without writing if statements?
$plugin_options = get_option('plugin_options'); $plugin_fonts_array = array('chunk', 'crimson', 'deja vu sans', 'deja vu sans condensed', 'deja vu mono', 'deja vu serif', 'deja vu serif condensed', 'im fell french canon', 'junction', 'lato light', 'lato regular', 'lato black', 'league gothic condensed', 'league gothic regular', 'league script number one', 'linux libertine', 'linux libertine display', 'linux libertine mono', 'ocrb', 'open baskerville', 'open sans light', 'open sans', 'open sans semi bold', 'open sans bold', 'prociono', 'raleway thin', 'source sans pro light', 'source sans pro light', 'source sans pro', 'source sans pro semi bold', 'source sans pro black'); foreach ($plugin_fonts_array $font) { if ($font == $plugin_options['dropdown1']) { $plugin_front_font = $plugin_options['dropdown1']; if ($plugin_front_font == 'chunk') { $plugin_front_font = 'chunk'; } if ($plugin_front_font == 'crimson') { $plugin_front_font = 'crimson'; } if ($plugin_front_font == 'deja vu sans') { $plugin_front_font = 'dejavusans'; } if ($plugin_front_font == 'deja vu sans condensed') { $plugin_front_font = 'dejavusanscondensed'; } if ($plugin_front_font == 'deja vu mono') { $plugin_front_font = 'dejavusansmono'; } if ($plugin_front_font == 'deja vu serif') { $plugin_front_font = 'dejavuserif'; } if ($plugin_front_font == 'deja vu serif condensed') { $plugin_front_font = 'dejavuserifcondensed'; } if ($plugin_front_font == 'im fell french canon') { $plugin_front_font = 'imfellfrenchcanon'; } if ($plugin_front_font == 'junction') { $plugin_front_font = 'junction'; } if ($plugin_front_font == 'lato light') { $plugin_front_font = 'latolight'; } if ($plugin_front_font == 'lato regular') { $plugin_front_font = 'latoregular'; } if ($plugin_front_font == 'lato black') { $plugin_front_font = 'latoblack'; } if ($plugin_front_font == 'league gothic condensed') { $plugin_front_font = 'leaguegothiccondensedregular'; } if ($plugin_front_font == 'league gothic regular') { $plugin_front_font = 'leaguegothicregular'; } if ($plugin_front_font == 'league script number one') { $plugin_front_font = 'leaguescriptnumberone'; } if ($plugin_front_font == 'linux libertine') { $plugin_front_font = 'linuxlibertine'; } if ($plugin_front_font == 'linux libertine display') { $plugin_front_font = 'linuxlibertinedisplay'; } if ($plugin_front_font == 'linux libertine mono') { $plugin_front_font = 'linuxlibertinemono'; } if ($plugin_front_font == 'ocrb') { $plugin_front_font = 'ocrb10'; } if ($plugin_front_font == 'open baskerville') { $plugin_front_font = 'openbaskerville'; } if ($plugin_front_font == 'open sans light') { $plugin_front_font = 'opensanslight'; } if ($plugin_front_font == 'open sans') { $plugin_front_font = 'opensans'; } if ($plugin_front_font == 'open sans semi bold') { $plugin_front_font = 'opensanssemibold'; } if ($plugin_front_font == 'open sans bold') { $plugin_front_font = 'opensansextrabold'; } if ($plugin_front_font == 'prociono') { $plugin_front_font = 'prociono'; } if ($plugin_front_font == 'raleway thin') { $plugin_front_font = 'ralewaythin'; } if ($plugin_front_font == 'source sans pro light') { $plugin_front_font = 'sourcesansproextralight'; } if ($plugin_front_font == 'source sans pro light') { $plugin_front_font = 'sourcesansprolight'; } if ($plugin_front_font == 'source sans pro') { $plugin_front_font = 'sourcesanspro'; } if ($plugin_front_font == 'source sans pro semi bold') { $plugin_front_font = 'sourcesansprosemibold'; } if ($plugin_front_font == 'source sans pro black') { $plugin_front_font = 'sourcesansproblack'; } } }
i'd make associative array instead:
$plugin_options = get_option('plugin_options'); $font_map = array( 'chunk' => 'chunk', 'crimson' => 'crimson', 'deja vu sans' => 'dejavusans', 'deja vu sans condensed' => 'dejavusanscondensed', 'deja vu mono' => 'dejavusansmono', 'deja vu serif' => 'dejavuserif', 'deja vu serif condensed' => 'dejavuserifcondensed', 'im fell french canon' => 'imfellfrenchcanon', 'junction' => 'junction', 'lato light' => 'latolight', 'lato regular' => 'latoregular', 'lato black' => 'latoblack', 'league gothic condensed' => 'leaguegothiccondensedregular', 'league gothic regular' => 'leaguegothicregular', 'league script number one' => 'leaguescriptnumberone', 'linux libertine' => 'linuxlibertine', 'linux libertine display' => 'linuxlibertinedisplay', 'linux libertine mono' => 'linuxlibertinemono', 'ocrb' => 'ocrb10', 'open baskerville' => 'openbaskerville', 'open sans light' => 'opensanslight', 'open sans' => 'opensans', 'open sans semi bold' => 'opensanssemibold', 'open sans bold' => 'opensansextrabold', 'prociono' => 'prociono', 'raleway thin' => 'ralewaythin', 'source sans pro light' => 'sourcesansproextralight', 'source sans pro light' => 'sourcesansprolight', 'source sans pro' => 'sourcesanspro', 'source sans pro semi bold' => 'sourcesansprosemibold', 'source sans pro black' => 'sourcesansproblack' ); $dropdown_font = $plugin_options['dropdown1']; if (array_key_exists($dropdown_font, $font_map)) { $plugin_front_font = $font_map[$dropdown_font]; } else { echo "invalid font"; }
although since of font names lowercase , without punctuation, make generic function , account special cases.
either way, code complicated. why need this?
Comments
Post a Comment