I am utilizing a wordpress wordpress plugin for advanced form design/logic/processing, and something of it's (greatest) disadvantages may be the awkward way it offers form data. Information is succumbed an assortment with 2 or 3 products for every area.

whether it's a concealed area:

  1. $$$n => id - where n may be the field's order within the form, and Title may be the title/id.
  2. id => value - where id may be the title/id and cost may be the value.

Whether it's an obvious area:

  1. $$$n => LABEL - where n may be the field's order within the form, and LABEL may be the human-readable label.
  2. $$$id => LABEL - where id may be the title/id from the area
  3. LABEL => value - where value is exactly what I really want.

I am attempting to write a function which will consume this horrid array and return a less complicated one having a single id => value pair for every area.

For instance, it will require this (order of fields can not be guaranteed):

array(
    '$$$1' => 'command',
    'command' => 'signup',
    '$$$2' => 'First Name',
    '$$$firstname' => 'First Name',
    'First Name' => 'John',
    '$$$3' => 'Email Address',
    '$$$email' => 'Email Address',
    'Email Address' => 'john@example.com'
);

And return:

array(
    'command' => 'signup',
    'email' => 'john@example.com',
    'firstname' => 'John'
);

This code works, but feels very awkward. Are you able to help refine it? Thanks! (My technique is to disregard everything however the $$$n fields since they are identical for those forms and there is no simple method to know if confirmed area is hidden or otherwise.)

function get_cforms_data($cformsdata) {
$data = array();
foreach ($cformsdata as $key => $value) {
    if (strpos($key, '$$$') === 0) {
        $newkey = substr($key, 3);
        if (is_numeric($newkey)) {
            $keys = array_keys($cformsdata, $value);
            if (count($keys) == 1) {
                // must be a hidden field - NAME only appears once
                $data[$value] = $cformsdata[$value];
            } else {
                // non-hidden field.  Isolate id.
                foreach($keys as $k) {
                    if ($k == $key) {
                        // $$$n - ignore it
                        continue;
                    } else {
                        // $$$id
                        $k = substr($k, 3);
                        $data[$k] = $cformsdata[$value];
                    }
                }
            }
        }
    }
}
return $data;
}

Here is a working version:

$i=0;
$result = array();

while (array_key_exists('$$$' . ++$i, $arr)) {
    $key = $arr['$$$' . $i];
    $result[$key] = $arr[$key];
}

It'll print:

Array
(
    [command] => signup
    [First Name] => John
    [Email Address] => john@example.com
)

Here's another version. It's much uglier but does exactly what you would like (when i construed it)

function get_cforms_data($cformsdata) {
    $i=0;
    $result = array();
    $flipped = array_flip($arr);

    while (array_key_exists('$$$' . ++$i, $arr)) {
        $key = $arr['$$$' . $i];

        $realKey = (!preg_match('~^\${3}\d+$~', $flipped[$key])) ? 
                    substr($flipped[$key], 3) : $key;

        $result[$realKey] = $arr[$key];
    } 
    return $result;   
}

Returns:

Array
(
    [command] => signup
    [firstname] => John
    [email] => john@example.com
)

View it working: http://ideone.com/5mkbx

This is what I'd most likely do, though I do not think it's much cleaner than yours or NullUserException's:

function get_cforms_data($cforms_data) {
    $ret = array();
    $label_to_id = array();
    $label_or_id_to_value = array();
    foreach ($cforms_data as $key => $value) {
        if (substr($key, 0, 3) === "$$$") {
            if (!is_numeric($id = substr($key, 3))) {
                $label_to_id[$value] = $id;
            }
        } else {
            $label_or_id_to_value[$key] = $value;
        }
    }
    foreach ($label_or_id_to_value as $key => $value) {
        $ret[isset($label_to_id[$key]) ? $label_to_id[$key] : $key] = $value;
    }
    return $ret;
}