Judging by your other SO account's question using my previous coding attempt with your sample $_POST
data, this should be right for your task:
Method #1: (The non-regex / more stable way)
$_POST=[
'authToken'=>'0a65e943412453ecec35c814',
'sessionId'=>'431503466924',
'answers' => '[{"Boost":false,"answerTime":1300,"id":3},{"Boost":false,"answerTime":800,"id":1},{"Boost":false,"answerTime":900,"id":3},{"Boost":false,"answerTime":1000,"id":1},{"Boost":false,"answerTime":1200,"id":1}]',
'userId' =>'2235'
];
$time=[800,900,1000,1100,1200,1500];
$answers=json_decode($_POST['answers'],true); // convert "answers" value to an array
foreach($answers as &$a){ // iterate each subarray
$a['answerTime']=$time[array_rand($time)]; // replace the previous answerTime value with a new random one
}
$_POST['answers']=json_encode($answers); // apply updated & re-encoded "answers" string to $_POST
var_export($_POST);
Method #2: (The regex / less stable way)
$_POST=[
'authToken'=>'0a65e943412453ecec35c814',
'sessionId'=>'431503466924',
'answers' => '[{"Boost":false,"answerTime":1300,"id":3},{"Boost":false,"answerTime":800,"id":1},{"Boost":false,"answerTime":900,"id":3},{"Boost":false,"answerTime":1000,"id":1},{"Boost":false,"answerTime":1200,"id":1}]',
'userId' =>'2235'
];
$time=[800,900,1000,1100,1200,1500];
$_POST['answers']=preg_replace_callback('/answerTime":Kd+/',function($m)use($time){return $time[array_rand($time)];},$_POST['answers']);
var_export($_POST);
The K
in the regex pattern says: "start the fullstring match from here", then it matches only the digits that follow answerTime:
.
Possible output with either method:
array (
'authToken' => '0a65e943412453ecec35c814',
'sessionId' => '431503466924',
'answers' => '[{"Boost":false,"answerTime":1200,"id":3},{"Boost":false,"answerTime":1000,"id":1},{"Boost":false,"answerTime":1500,"id":3},{"Boost":false,"answerTime":900,"id":1},{"Boost":false,"answerTime":800,"id":1}]',
'userId' => '2235',
)