PHP SUM IF具有列标准的函数

时间:2017-07-03 22:08:55

标签: php json

我想找到一个公式,用Surveyydone开头汇总我的所有列。在我的例子中,我有一些以surveydone开头的列。能帮我找到这个配方吗?谢谢

请在下面找到我在PHP中使用的echo json_encode:

{"id":"7","username":"Thomas","survey1":"valide","surveytoken1":"EuLwfqJI5UYLgK2LQ7jS","surveytokenvalid1":"1","surveydone1":"1","surveytoken2":"EuLwfqJI5UYLgK2LQ7jS","surveytokenvalid2":"1","surveydone2":"1" [...] "surveytokenvalid10":"1","surveydone10":"1"}

这是我在php中的代码:

   $user_username = $_SESSION['auth']->username;
   $user['username'] = $user_username;       
   $req = $pdo->prepare('SELECT * FROM data WHERE (username = ?)');
   $req->execute([$user_username]);
   $surveyuser = $req->fetch();
   echo json_encode($surveyuser);

1 个答案:

答案 0 :(得分:0)

这是我设法找到的答案,并且它有效。

   $user_username = $_SESSION['auth']->username;
   $user['username'] = $user_username;       
   $req = $pdo->prepare('SELECT * FROM data WHERE (username = ?)');
   $req->execute([$user_username]);
   $surveyuser = $req->fetch();
   echo json_encode($surveyuser);

  $surveyuserarray = json_decode(json_encode($surveyuser), True);
  $surveyuser2 = json_encode($surveyuser);

  foreach($surveyuserarray as $key => $value){
    if (strpos($key, 'surveydone') === 0) {
        $only_point[$key] = $value;
       }
   }

  $total_points=array();
  $total_points['totalpoints']=array_sum($only_point);

   echo ($surveyuser2);
   echo json_encode($total_points);