Array_push在for循环中给出空响应

时间:2018-08-29 05:03:17

标签: php arrays

你好,我试图使用array_push推入一个数组,但是我得到了第一个索引的值,然后所有我得到的都是空响应,没有得到我做错的地方。值正确,但在array_push中存在for循环错误。

这是我的代码:

  function actioncouponcsv_download() {
    $this->layout = false;
    foreach (Yii::app()->log->routes as $route) {
        if ($route instanceof CWebLogRoute || $route instanceof CFileLogRoute || $route instanceof YiiDebugToolbarRoute) {
            $route->enabled = false;
        }
    }
    $dateRange = json_decode($_POST['dateRange'], true);
    $start = $dateRange['start'];
    $end = $dateRange['end'];
    $validity = $_POST['validity'];
    $limit = isset($_REQUEST['limit']) && trim($_REQUEST['limit']) ? $_REQUEST['limit'] : 0;
    $studio_id = Yii::app()->user->studio_id;
    if (isset($_GET['type']) && intval($_GET['type']) ==2) {
            $couponobj = new CouponSubscription();
            $getcouponobj = $couponobj->getcoupon_data($studio_id,$start,$end,$validity);
        $k = 0;
            $title_addon = "\t" . "Discount Cycle" . "\t" . "Extend free trail";
        $data_addon = "\t" . $getcouponobj[$k]['discount_multiple_cycle'] . "\t" . $getcouponobj[$k]['extend_free_trail'];
    } else {
        $title_addon = "";
        $data_addon = "";
        $couponobj = new Coupon();
        $getcouponobj = $couponobj->getcoupon_data($studio_id,$limit,1);
    }

    //$Coupon = Coupon::model()->find('studio_id=:studio_id', array(':studio_id' => $studio_id));
    $dataCsv = '';
    if ($getcouponobj) {
        $headings = "Sl no" . "\t" . "Coupon" . "\t" . "Coupon Type" . "\t" . "Used by a single user" . "\t" . "Valid" . "\t" . "Used" . "\t" . "User" .$title_addon. "\t" . "Used Date". "\t" . "Content Category". "\t" . "Content"."\n";
        $i = 1;
        $dataCSV[] = Array();
        $j = 0;
        for ($k = 0; $k < count($getcouponobj); $k++) {
            $userList = '-';
            if ($getcouponobj[$k]['used_by'] != '0') {
                if ($getcouponobj[$k]['coupon_type'] == 1) {
                    $userList = '';
                    $userIdList = explode(",", $getcouponobj[$k]['used_by']);
                    foreach ($userIdList as $userIdListKey => $userIdListVal) {
                        if ($userIdListKey == 0) {
                            $userList .= Yii::app()->webCommon->getuseremail($userIdListVal);
                        } else {
                            $userList .= " | " . Yii::app()->webCommon->getuseremail($userIdListVal);
                        }
                    }
                } else {
                    $userList = Yii::app()->webCommon->getuseremail($getcouponobj[$k]['used_by']);
                }
            }

            if($getcouponobj[$k]['is_all']!=1){
                if($getcouponobj[$k]['content_category']==1){
                    $cont_cat = "Digital";
                    $content_str = Coupon::model()->getContentInfo($getcouponobj[$k]['specific_content']);
                    $cont_str = $content_str;
                }else if($getcouponobj[$k]['content_category']==2){
                    $cont_cat = "Physical";
                    $content_str = Coupon::model()->getContentInfoPhysical($getcouponobj[$k]['specific_content']);
                    $cont_str = $content_str;
                }else{
                    $cont_cat = "All";
                    $cont_str = "All";
                }
            }else{
                $cont_cat = "All";
                $cont_str = "All";
            }
            #echo $getcouponobj[$k]['coupon_code'];
              array_push($dataCSV[$j],$i);
              array_push($dataCSV[$j],$getcouponobj[$k]['coupon_code']);
              array_push($dataCSV[$j],(($getcouponobj[$k]['coupon_type'] == 1) ? 'Multi-use' : 'Once-use'));
              array_push($dataCSV[$j],(($getcouponobj[$k]['user_can_use'] == 1) ? 'Multiple times' : 'Once'));
              array_push($dataCSV[$j],(($getcouponobj[$k]['used_by'] == 0) ? 'Yes' : 'No'));
              array_push($dataCSV[$j],(($getcouponobj[$k]['used_by'] == 0) ? '-' : 'Yes'));
              array_push($dataCSV[$j],$userList);
              array_push($dataCSV[$j],$getcouponobj[$k]['discount_multiple_cycle']);
              array_push($dataCSV[$j],$getcouponobj[$k]['extend_free_trail']);
              array_push($dataCSV[$j],(($getcouponobj[$k]['cused_date'] == 0) ? '-' : $getcouponobj[$k]['cused_date']));
              array_push($dataCSV[$j],$cont_cat);
              array_push($dataCSV[$j],$cont_str);
            $j++;


            //$dataCsv .= $i . "\t" . $getcouponobj[$k]['coupon_code'] . "\t" . (($getcouponobj[$k]['coupon_type'] == 1) ? 'Multi-use' : 'Once-use') . "\t" . (($getcouponobj[$k]['user_can_use'] == 1) ? 'Multiple times' : 'Once') . "\t" . (($getcouponobj[$k]['used_by'] == 0) ? 'Yes' : 'No') . "\t" . (($getcouponobj[$k]['used_by'] == 0) ? '-' : 'Yes') . "\t" . $userList .$data_addon. "\t" . (($getcouponobj[$k]['cused_date'] == 0) ? '-' : $getcouponobj[$k]['cused_date'])."\t" .$cont_cat ."\t".$cont_str."\n";

            $i = $i+1;
        }
    }
    print_r(json_encode($dataCSV));

}

PS:我正在获取价值。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

好吧,我首先看到的错误是声明数组的方式:

$dataCSV[] = Array();

$ array [] =表示您正在向现有数组添加新值。要声明数组,应使用

$dataCSV = array();

此外,此代码:

array_push($dataCSV[$j],$i);

意味着您要向$ dataCSV [$ j]数组中添加一个新值,但是永远不会将其声明为数组,因此首先要做的是

$dataCSV[$j] = new array();

您的代码确实很长而且很复杂,这些只是我在其中看到的问题的示例。