There is a "item" field, which contains: "0; 0; 0". Question: how can I change in the request, for example, only the second zero by 1 or 2 by php, respectively?

  • Or maybe you need to immediately change the database without php? - Ale_x
  • No need to store many values ​​in one field. You are welcome. - etki
  • I agree with @Etki, it is better to store the values ​​in different columns than to suffer with separators, moreover, if you store in different columns, you can easily do sorting by different columns, which cannot be done with a delimited string ... - Nik555
  • @ Nik555, it just turns out 3 extra fields - XenK
  • @XenK, as if in this data do not save in another field. As if this does not work out a lot of excess hemorrhoids. - etki

2 answers 2

A simple solution is to use the php function: explode("Разделитель", "строка") .

For example:

 $str = "0;0;0" $arrElement = explode(";", $str); 

Now the variable (array) $arrElement contains three elements.
And if you, for example, want to correct the second value and write down the date, then you can do it like this:

 $arrElement[1] = 2; //Поменяли значение на 2. $newStr = implode(';',$arrElement); 

PS This option is easy to understand and implement, but not the best solution. The best solution is to write a regular expression.

    sample of $ item

     $items = explode(';', $item); $items[1] = $items[1]+1; $item = implode(';',$items); 

    write to bd $ item