User login

You can pass part of an array variable by reference in PHP5

Pretty nifty. PHP5 (and PHP4 for all I know) lets you pass not merely an entire variable, but part of a variable that is an array (and has nested arrays). Passing a variable by reference allows you to make changes directly to that variable in a function without having to figure out how to add those changes back-- the changes are made in the original variable, not in a copy (which is how variables passed to a function typically operate).

Example number one: $form['taxonomy']

<pre>
function wsf_action_form_alter($form_id, &$form) {
  switch ($form_id) {  // not $form['#id']
    case 'profile_node_form':
      $form['body_filter']['body']['#rows'] = 5;
      wsf_action_fa_remove_taxonomy_fieldset($form['taxonomy']);
      break;
...
  }
}
</pre>

And here is the function that receives the taxonomy subset of the form array by reference:

<pre>
function wsf_action_fa_remove_taxonomy_fieldset(&$form_taxonomy) {
  unset($form_taxonomy['#type']);
  unset($form_taxonomy['#title']);
  unset($form_taxonomy['#collapsible']);
  unset($form_taxonomy['#collapsed']);
  return;
}

No more annoying "Categories" title and collapsible fieldset if you don't want it!

Agaric tested and approved.

(Note to self: it might be advisable to put an if statement in there to check if there is a grouping of fields set before unsetting everything. Then again, maybe PHP is OK with the direct route.)

Resolution

Searched words: 
php pass part of an array by reference

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.