Click here to Skip to main content
15,881,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi2all
i want to search for first number after comma and if match found replace second number with another number i specify it

my code is

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';
$resouces_array=explode(',', $value);

foreach ($resouces_array as $key=> $value) {
    $first_number  =substr($value,0,2);
    $second_number   = explode(' ', $value);

    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 the output to be like that
$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 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 my code produce that

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,


so what is wrong and is there any simple solution other mine ??
Posted

1 solution

PHP
<?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';
$resouces_array=explode(',', $value);

$output = '';
foreach ($resouces_array as $key => $value) {
    $number   = explode(' ', $value);

     if($number[0] == 23){
         $number[1] = 50;
     }

     $output .= implode(" ", $number). ",";
}

echo rtrim($output,",");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900