modify number in string php separated by comma -


i have string of numbers. numbers appear in sets of three. change second number in set depending on first number in set.

this code far.

$value='1 0 0,4 2 0,1 20 0,3 0 0,2 0 0,2 0 0,3 0 0,4 0 0,4 0 0,3 0 0,3 0 0,4 0 0,4 0 0,1 0 0,4 0 0,2 0 0,1 0 0,2 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,10 20 0,15 20 0,0 0 0,0 0 0,11 20 0,23 10 0,0 0 0,0 0 0,27 7 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,16 1 0,0 0 0'; $resouces_array=explode(',', $value);  foreach ($resouces_array $key=> $value) {     $first_number  =substr($value,0,2);     $second_number   = explode(' ', $value);      // part specify second number should depending on first number.     if($first_number == 23)     {         $second_number[1]= 50;     }      //$first_parts= explode(' ', $value);     $string_valo= implode(' ' ,$second_number);     $after_spli=str_pad($string_valo, 6,',');     echo $after_spli; } 

i want output this.

1 0 0,4 2 0,1 20 0,3 0 0,2 0 0,2 0 0,3 0 0,4 0 0,4 0 0,3 0 0,3 0 0,4 0 0,4 0 0,1 0 0,4 0 0,2 0 0,1 0 0,2 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,10 20 0,15 20 0,0 0 0,0 0 0,11 20 0,23 50 0,0 0 0,0 0 0,27 7 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,16 1 0,0 0 0'; 

but code produces this.

1 0 0,4 2 0,1 20 03 0 0,2 0 0,2 0 0,3 0 0,4 0 0,4 0 0,3 0 0,3 0 0,4 0 0,4 0 0,1 0 0,4 0 0,2 0 0,1 0 0,2 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,10 20 015 20 00 0 0,0 0 0,11 20 023 50 00 0 0,0 0 0,27 7 00 0 0,0 0 0,0 0 0,0 0 0,0 0 0,16 1 00 0 0, 

<?php  $value='1 0 0,4 2 0,1 20 0,3 0 0,2 0 0,2 0 0,3 0 0,4 0 0,4 0 0,3 0 0,3 0 0,4 0 0,4 0 0,1 0 0,4 0 0,2 0 0,1 0 0,2 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,10 20 0,15 20 0,0 0 0,0 0 0,11 20 0,23 10 0,0 0 0,0 0 0,27 7 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,16 1 0,0 0 0'; echo $value . '</br>'; $resouces_array=explode(',', $value);  $output = array();  foreach ($resouces_array $value) {     $numbers = explode(' ', $value);      if($numbers[0] == 23)     {         $numbers[1] = 50;     }      $output[] = implode(' ', $numbers);  }  $output = implode(',', $output); echo $output;  ?> 

the problem had not deconstructing , rebuilding array of arrays. above answer problem double explode based on , , double implode of delimiters. in loop check first item of 1 of arrays modifes 2nd number of array. put in near function format reuse quickly.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -