从csv格式的PHP中删除JSON_EXTRACT中的逗号

时间:2017-03-24 10:13:24

标签: php json csv

当我从csv格式的db中提取文件后,每个名字我都会收到逗号,我不知道他们在哪里。查看屏幕截图:how the csv file looks

这是代码:

function expired(){
  if(isset($_POST['download']))
       {

           $db =& JFactory::getDBO();
           $time = (time());
           $query = "select user_id, profile_value FROM #__user_metadata WHERE JSON_EXTRACT(profile_value, '$.expiry_date') > '$time'";
            $db->setQuery($query);

             $rows = $db->loadObjectList();

               header('Content-type: text/csv');
                ob_end_clean(); // is cleaning the space before the list

                 header('Content-Disposition: attachment; filename=UserDatabase.csv');
                 ob_start();
                $output = fopen("php://output", "w");

             foreach ($rows as $row) {

                $user_id = json_decode($row->user_id);

                 $db =& JFactory::getDBO();
                $query = "select user_id, profile_value FROM #__user_metadata WHERE user_id = '$user_id'" ;
                $db->setQuery($query);

                $rows = $db->loadObjectList();


                foreach ($rows as $row) {

                $profile_value = json_decode($row->profile_value);


                $first_name=$profile_value->first_name;

                $last_name=$profile_value->last_name;
                $email =$profile_value->email;

                $users = array($first_name . " " . $last_name, $email);
               fputcsv($output, $users, ',') ;

                   }

        }
         fclose($output);
            ob_end_flush();
            exit(0);

}

}

看起来json_extract占用了db中的所有用户,但没有显示它们,只是一个由昏迷分隔的空槽,问题不在于该行:

fputcsv($output, $users, ',') ;

1 个答案:

答案 0 :(得分:0)

我必须更改所有代码才能将其删除。这是解决方案: 也许你会找到一些有用的东西。问题是在变量$ users中我有一个双数组。

   $db =& JFactory::getDBO();
    $app = JFactory::getApplication(); //
     $user_type= $app->input->getString('user_type');


       switch($user_type){

        case  'Export expired danish users':

         $query = "select user_id, profile_value FROM #__user_metadata WHERE profile_key = 'profile20.subscription_shared' AND JSON_EXTRACT(profile_value, '$.expiry_date') > '$time'";
               $rows = $db->loadObjectList();




         break;

         case 'foreign':
            $query = "select user_id, profile_value FROM #__user_metadata WHERE profile_key = 'profile20.subscription_shared' AND JSON_EXTRACT(profile_value, '$.expiry_date') > '$time'";
               $rows = $db->loadObjectList();
               break;
             default:

           $db =& JFactory::getDBO();
          $query = "select user_id, profile_value FROM #__user_metadata WHERE profile_key = 'profile20.subscription_shared' AND JSON_EXTRACT(profile_value, '$.expiry_date') > '$time'";
            $db->setQuery($query);

            break;

   }

        header('Content-type: text/csv');
        ob_end_clean(); // is cleaning the space before the list

        header('Content-Disposition: attachment; filename=UsersWithExpiredDate.csv');
        ob_start();
        $output = fopen("php://output", "w");

        foreach ($rows as $row) {

            $user_id = $row->user_id;

             $query = "SELECT JSON_unquote(JSON_EXTRACT(profile_value, '$.email')) AS email, JSON_unquote(JSON_EXTRACT(profile_value, '$.first_name')) AS first_name, JSON_unquote(JSON_EXTRACT(profile_value, '$.last_name')) AS last_name FROM nagwm_user_metadata WHERE user_id = '$user_id ' AND profile_key = 'profile20.profile_data'";

            $db->setQuery($query);

            $rows = $db->loadRow();

            $first_name=$rows[1];


            $last_name=$rows[2];
            $email =$rows[0];

            $users = array($first_name . " " . $last_name, $email); 
            fputcsv($output, $users) ;  

        }
            fclose($output);
            ob_end_flush();
            exit(0);