Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP convert $_POST values to string

Tags:

php

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.

like image 901
bikey77 Avatar asked Apr 23 '26 16:04

bikey77


1 Answers

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); 
like image 155
dotoree Avatar answered Apr 26 '26 05:04

dotoree