Home > Mobile >  Pass Array through Function, Update Values
Pass Array through Function, Update Values

Time:10-11

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.

CodePudding user response:

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']);
  •  Tags:  
  • php
  • Related