【问题标题】:Laravel 5.1 Get Input ValueLaravel 5.1 获取输入值
【发布时间】:2016-07-11 09:26:50
【问题描述】:

我正在创建一个这样的 Xml:

private function setTitle()
{
    $rC        = $this->data->rC;
    $cTimes    = array();
    $i         = 0;

    foreach ($rC as $rCKey => $rCValue)
    {
        $cTimes[] = $rCValue->input_c_start;
    }

    foreach ($rC as $rCKey => $rCValue)
    {
        $this->title = $this->titleSetTitles->addChild('title');
        $this->title->addAttribute('chapters', $cTimes[$i]);
        $i++;
    }

}

输出是这样的:

  <title chapters="00:01">
    ...
  </title>
  <title chapters="00:02">
    ...
  </title>

我现在的问题是如何在每个循环中添加章节时间,使其看起来像这样:

  <title chapters="00:01">
    ...
  </title>
  <title chapters="00:01; 00:02">
    ...
  </title>
  <title chapters="00:01; 00:02; 00:03">
    ...
  </title>

【问题讨论】:

    标签: php xml foreach


    【解决方案1】:

    在 foreach 循环之外有另一个变量,它会在每次迭代时附加数据。

    private function setTitle()
    {
        $rC        = $this->data->rC;
        $cTimes    = array();
        $i         = 0;
        $chapters  = "";
    
        foreach ($rC as $rCKey => $rCValue)
        {
            $cTimes[] = $rCValue->input_c_start;
        }
    
        foreach ($rC as $rCKey => $rCValue)
        {
            $chapters .= $cTimes[$i]."; ";
            $this->title = $this->titleSetTitles->addChild('title');
            $this->title->addAttribute('chapters', substr($chapters, 0, -2));
            $i++;
        }
    
    }
    

    【讨论】:

    • 有效,但最后我得到一个空格,你将如何制定一个条件来防止这种情况:chapters="00:00:00; 00:10:00; "&gt;
    • @JohnDoe2 Remove last 2 chars with substr 查看我编辑的答案
    【解决方案2】:

    有一个额外的变量并像这样使用:-

    private function setTitle(){
        $rC        = $this->data->rC;
        $cTimes    = array();
        $i         = 0;
    
        foreach ($rC as $rCKey => $rCValue){
            $cTimes[] = $rCValue->input_c_start;
        }
        $ncTime = [];
        foreach ($rC as $rCKey => $rCValue){
            $ncTime[] = $cTimes[$i];
            $this->title = $this->titleSetTitles->addChild('title');
            $this->title->addAttribute('chapters', implode(";", $ncTime));
            $i++;
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 2023-03-19
      相关资源
      最近更新 更多