0

I want to update the 'meet_time' in an array if certain conditions are met. I retrieve the result-set successfully:

$subject_set = get_array();
$results = mysqli_fetch_all($subject_set, MYSQLI_ASSOC);

And then it passes through a function to try and update values:

function process_values($results) {
  foreach($results as $k=>$v) {
    if ($v['sun'] == '1') {
      array_replace($results, [$v['meet_time']=>'1800']);
    }
    if ($v['mon'] == '1') {
      array_replace($results, [$v['meet_time']=>'1200']);
    }
    $b[] = $v['meet_time'];
  }
  asort($b);
  foreach ($b as $k=>$v) {
    $c[] = $results[$k];
  }
  return $c;
}

$sorted = process_values($results);

I'd like to work with this new array:

foreach ($sorted as $row) {
    echo $row['meet_time'] . "<br />;
}

Expected results would be:

1200
1800

But they remain the same as they were in the original result-set from the database (that were neither 1200 nor 1800). I can asort() on any value I put into $b[] but it's not being impacted by the array_replace() function. I'm not getting any errors to suggest what I'm doing wrong.

1
  • Could you share a sample of the $results array? Commented Oct 10, 2021 at 18:33

1 Answer 1

1

You could just assign the replaced value instead of using array_replace

Replace

array_replace($results, [$v['meet_time']=>'1800']);

With

$results[$k]['meet_time'] = '1800'; //1200 or whatever value you want

OR

  array_replace($results[$k], [$v['meet_time']=>'1800']);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.