如何用php替换json数组中的值


how to replace value in json array with php

我想通过传递给函数来替换"created_at"。这是我的函数

 public function getfeeds($showvalue){
      $stmt = $this->con->prepare("SELECT id,image,title,status,profilepic,created_at,url FROM news ORDER BY id DESC ");
      $stmt->bind_param("s",$showvalue);
      $stmt->execute();
      $result = $stmt->get_result();
      $nrow = array();
    while ($r = $result->fetch_assoc()) {
       $nrow[] = $r;
    }
       $frow['news'] = $nrow;
    $json = str_replace("''/", "/",json_encode($frow));
    return $json;
   }

我想通过传递给函数converttime($time)来替换创建的值

my convert time function

function converttime($time, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($time);
    $diff = $now->diff($ago);
    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;
    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }
    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

请帮助我如何转换它

让我们直说吧…

  1. 变量呼叫函数:

    $varofcall = $this->converttime($param);
    

  2. 使用变量

    public function getfeeds($showvalue){
        $varofcall = $this->converttime(param);
        $stmt = $this->con->prepare("SELECT id, image, title, status, profilepic, $varofcall ,url FROM news ORDER BY id DESC ");