Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8377741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:44:52+00:00 2026-06-09T15:44:52+00:00

I have the following PHP function: public function createOptions($options, $cfg=array()) { $cfg[‘methodKey’] = isset($cfg[‘methodKey’])

  • 0

I have the following PHP function:

    public function createOptions($options, $cfg=array()) {
        $cfg['methodKey'] = isset($cfg['methodKey']) ? $cfg['methodKey'] : 'getId';
        $cfg['methodValue'] = isset($cfg['methodValue']) ? $cfg['methodValue'] : 'getName';
        $cfg['beforeKey'] = isset($cfg['beforeKey']) ? $cfg['beforeKey'] : '';
        $cfg['beforeValue'] = isset($cfg['beforeValue']) ? $cfg['beforeValue'] : '';
        $cfg['afterKey'] = isset($cfg['afterKey']) ? $cfg['afterKey'] : '';
        $cfg['afterValue'] = isset($cfg['afterValue']) ? $cfg['afterValue'] : '';
        $array = array();
        foreach ($options as $obj) {
            $array[$cfg['beforeKey'] . $obj->$cfg['methodKey']() . $cfg['afterKey']] = $cfg['beforeValue'] . $obj->$cfg['methodValue']() . $cfg['afterValue'];
        }
        return $array;
}

It’s something I use around my app to create select boxes from array data. I just recently added 4 new $cfg variables for adding strings before or after the key and value of the select box. So for example, if my drop down list looked like “A, B, C” by default, I can pass:

$cfg['beforeValue'] = 'Select ';
$cfg['afterValue'] = ' now!';

and get “Select A now!, Select B now!, Select C now!”

So this works fine, but I was wondering if there was some way in PHP to accomplish this in one line sorta speak rather than two. I figure there must be a special way to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:44:54+00:00Added an answer on June 9, 2026 at 3:44 pm

    First, simplify that horrendous code with this:

    public function createOptions($options, array $cfg = array()) {
        $cfg += array(
            'methodKey'   => 'getId',
            'methodValue' => 'getName',
            ...
        );
    

    No need for all the isset and repeated key names, a simple array union will do.

    Secondly, you can use something like sprintf:

    $cfg['surroundingValue'] = 'Select %s now!';
    echo sprintf($cfg['surroundingValue'], $valueInTheMiddle);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: <?php function foo($bar) { global $products; //$products = array();
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
In my CouponsController.php, I have the following 2 functions: public function index() { $this->set('coupons',
I have the following in php: public function myfunction($v){ //ini_set(memory_limit,32M); $v=mysqli_real_escape_string($this->connection,$v); $stmt=mysqli_prepare($this->connection,SELECT * from
I have the following test case: include_once('../Logger.php'); class LoggerTest extends PHPUnit_Framework_TestCase { public function
I have following php class class User implements IUser { public function __construct($i_objParent =
Suppose we have following function in PHP: public function testSomething() { $name = perform_sql_query("SELECT
I have two controllers: test.php public function trackback() { $this->load->library('trackback'); $tb_data = array( 'ping_url'
I have the following: somefile.php $(document).ready(function() { var handler = function(data) { var JsDiv
I have the following class in a php file: function calcTotal(){ var productID =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.