views:

583

answers:

5

eg: existing data retrieve from db field: a,b,c,d,e so now $data= "a,b,c,d,e";

eg: new data to be added--- cc, gg final value saved into db will be $finaldatatobeupdatedintodb= "a,b,c,d,e,cc,gg";

Retrieve out and add in few more values.

Then append it back into the comma list.

How to do it purely with implode and explode, without needing to put it into array to for loop to reconstruct/re-add the thing with new values added?

+2  A: 
$finaldatatobeupdatedintodb = $data . ',cc,gg';

Is that what you need? It's difficult to understand exactly what your question is asking.

Rob
+4  A: 

If you really want to use explode and implode, you could do something like this :

First, explode the string you have :

$data= "a,b,c,d,e";
$list = explode(',', $data);
var_dump($list);

Which will give you :

array
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)
  2 => string 'c' (length=1)
  3 => string 'd' (length=1)
  4 => string 'e' (length=1)

Then, add the new elements :

$to_add = array('cc', 'gg');
$new_list = array_merge($list, $to_add);
var_dump($new_list);

$new_list is now :

array
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)
  2 => string 'c' (length=1)
  3 => string 'd' (length=1)
  4 => string 'e' (length=1)
  5 => string 'cc' (length=2)
  6 => string 'gg' (length=2)

And, finally, implode the $new_list, using ',' as a separator :

$output = implode(',', $new_list);
var_dump($output);

And you get :

string 'a,b,c,d,e,cc,gg' (length=15)

Of course, if you start with an array, that's one less explode to do ; and if the data you want to add is not an array, that's one more explode to do...


But, as Rob pointed out, in the simple case you are presenting, there is no need for such a complicated piece of code : strings concatenations will be more than enough ;-)

The advantage with the array/explode/implode-based solution is that you can work on the final array before imploding it into a string (say, for instance, you can sort it)

Pascal MARTIN
A: 
<?

function addvalues($data, $value) {

  $array = Array();
  $array = explode(",", $data);

  // if value are an array, merge it, or just add as element
  if(is_array($value))
    $array = array_merge($array, $value);
  else
    $array[] = $value;

  return implode(",", $array);
}

// original string
$data = "a,b,c,d,e";

echo "Old data = ".$data."<br>";

// see the function, it will work with both arrays and variables
$data = addvalues($data, Array("cc"));
$data = addvalues($data, "gg");

echo "New data = ".$data."<br>";

?>
Andrejs Cainikovs
+1  A: 

Similar to Rob's answer, but probably a bit more robust:

$final = $data . (strlen($data) ? "," : "") . "cc,gg";

or, if your extra values are coming in as an array:

$newValues = array("cc", "gg");
$final = $data . (strlen($data) ? "," : "") . implode(",", $newValues);
nickf
Better use `strlen` as `"0"` is evaluated to *false*.
Gumbo
good point! edited.
nickf
A: 

Can you give more details about why you need to store comma-separated values in a database? Normally you would use separate fields for different discrete values. If you have a fixed number of values you could instead use the SET field type.

Normally the best solution is to use a separate table. Wikipedia's age on Database normalization may help.

DisgruntledGoat