【问题标题】:how to print an array in php如何在php中打印一个数组
【发布时间】:2020-08-28 20:20:40
【问题描述】:

我想以特定方式在php中打印我的数组,希望您能帮助我,非常感谢。

public function getLineChartPromedio($format = 'Y-m-d'){
    $startDate = $this->input->post('startDate');
    $endDate = $this->input->post('endDate');
    // Declare an empty array 
    $arraydias = array(); 
    
    // Variable that store the date interval 
    // of period 1 day 
    $interval = new DateInterval('P1D'); 

    $realEnd = new DateTime($endDate); 
    $realEnd->add($interval); 

    $period = new DatePeriod(new DateTime($startDate), $interval, $realEnd); 
    $count = 0;
    // Use loop to store date into array 
    foreach($period as $date) {                  
        $fecha = $date->format($format);
        $arraydias [] = array ($fecha);
    } 
    
    $string = "";
    $week_array = "";
    $size = 0;
    foreach ($arraydias as $Array) {
        $string .= implode("','", $Array);
        $week_array = "'".$string."'";
    }
    echo json_encode($week_array);
    
}

这种方法的结果是:“'2020-07-082020-07-092020-07-102020-07-112020-07-12'”

我想要实现的是:“'2020-07-08','2020-07-09','2020-07-10','2020-07-11','2020-07-12' "

我做错了什么?我希望你能帮助我

【问题讨论】:

    标签: php arrays json printing


    【解决方案1】:

    保留大部分代码。工作示例:

    function getLineChartPromedio($format = 'Y-m-d'){
        $startDate = $this->input->post('startDate');
        $endDate = $this->input->post('endDate');
        // Declare an empty array 
        $arraydias = array(); 
        
        // Variable that store the date interval 
        // of period 1 day 
        $interval = new DateInterval('P1D'); 
    
        $realEnd = new DateTime($endDate); 
        $realEnd->add($interval); 
    
        $period = new DatePeriod(new DateTime($startDate), $interval, $realEnd); 
        // Use loop to store date into array 
        foreach($period as $date) {                  
            $fecha = $date->format($format);
            $arraydias[] = $fecha;
        } 
        
        $string = "'" . implode("','", $arraydias) . "'";
        echo json_encode($string);
    }
    

    测试链接:click

    【讨论】:

      【解决方案2】:

      我想这应该足够了:

      // Use loop to store date into array 
      foreach($period as $date) {                  
          $fecha = $date->format($format);
          // Note - no `array` usage here
          $arraydias [] = $fecha;
      } 
      
      echo json_encode($array_dias);
      

      【讨论】:

        【解决方案3】:

        您可以使用array_map 的组合用引号括住您的日期,并使用implode 在它们之间添加逗号:

        echo implode(',', array_map(fn($date) => "'{$date}'", $arraydias));
        

        如果你需要用实际的双引号括起来:

        echo '"', implode(',', array_map(fn($date) => "'{$date}'", $arraydias)), '"';
        

        这意味着您最初通常较早地将日期添加到数组中:

        $arraydias[] = $fecha;
        

        (而不是像你那样$arraydias [] = array($fecha);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-07-14
          • 1970-01-01
          • 2021-04-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多