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

时间:2016-11-07 16:41:33

标签: php json

我想替换" 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)

来替换创建的值

我的转换时间功能

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 个答案:

答案 0 :(得分:0)

让我们直截了当地......

  1. 调用变量中的函数:

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

  2. 使用var

    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 ");

  3. 它呢?