1. tpelek
  2. General
  3. Tuesday, February 20 2018, 09:16 PM
Hello I have filed that I want to update using PHP script using $query->update, the field type is select with Multiple selection ON.
In database I see the data is like this ["0","1","3"]
I made some tries but it’s not possible to add or remove just one value
For example, I can replace ["0","1","3"] with ["2"] but I cannot add the 2 in exist values and be ["0","1","2","3"]
There any way to add or remove a single value?
Thanks
Accepted Answer
admin Accepted Answer
Admin
Hi,
yes, this is a JSON format, so you use a PHP script like:
$raw_value = '["0","1","3"]';
$array_value = json_decode($raw_value);
if(!is_array($array_value)) $array_value = array();

// Add Value
$value_to_add = '2';
if(!in_array($value_to_add, $array_value)) // if is not already available in array
array_push($array_value,$value_to_add);

// Remove Value
$value_to_remove = '0';
if(in_array($value_to_remove, $array_value)) // if value to remove is available in array
$array_value=array_diff($array_value, array($value_to_remove));

$new_raw_value = json_encode($array_value);
  1. more than a month ago
  2. General
  3. # Permalink
tpelek Accepted Answer
Pro
Content Protected
  1. more than a month ago
  2. General
  3. # 1
  • Page :
  • 1


There are no replies made for this post yet.
However, you are not allowed to reply to this post.