bikey77 bikey77 - 13 days ago 7
PHP Question

PHP convert $_POST values to string

I'd like to be able to convert all post data to a string but keep only the values in a string variable.

So if my posted data looks like this:

Array ( [alloy] => Array ( [0] => K18 [1] => )
[color] => Array ( [0] => Gold [1] => )
[stone] => Array ( [0] => Diamond [1] => )
[dimension] => Array ( [0] => 3cm [1] => )
[button1] => Submit )


i'd like it to finally look like: $data = 'K18, color Gold, Diamond, 3cm';

I've tried serialize, imploding array, http_build_query but they're not what I need.

if it helps to get the whole idea, I'm collecting data from 4 groups of checkbox and radiobutton arrays and I'd like to put the selected values into a delimited string and save to my db.

Answer

Maybe this is what you need:

$s = array();
foreach ($_POST as $k => $v) {
  if (is_array($v)) {
    if ('color' === $k) {
      array_push($s, implode('', array($k, $v[0])));
    } else {
      array_push($s, $v[0]);
    }
  }
}
echo implode(', ', $s); 
Comments