1

I am creating some options in the functions.php file. I created a function that gets input on the front end, puts the values into a new array and returns them to the front end. I am trying to figure out how to return the values like an associative array. For example, I want to the array to return like this

Array ( [dribble_id] => dribble [google_id] => google [facebook_id] => facebook [twitter_id] => twitter ) 

the below function returns them like so

 Array ( [0] => dribble [1] => google [2] => facebook [3] => twitter ) 

Here is the function that returns them

 function ritual_get_options()
{
   $arg_list = func_get_args();

    $options = array();
    foreach( $arg_list as $key => $value){
    $options[] = ot_get_option($value);
}
    return $options;
}

Do you now how I can do this?

1

This is not really a Wordpress question but more PHP. To solve this problem, you need to use $value as your key as well plus _id

You can do something like this

$options[$value . '_id'] = ot_get_option($value);

EDIT

If you need $key to be the value of id, you can also try something like this

$options[$value . '_' . $key] = ot_get_option($value);

This will render something like

google_0 => google
google_1 => google
google_2 => google
  • I've updated my answer to make use of $key as id as well – Pieter Goosen Nov 2 '14 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.