2016-09-07 28 views
1

我有我的类函数,它拥有多个阵列,它看起来是这样的:这个从PHP阵列装饰串值

protected function getTextItems() 
{ 
    if($this->hasChapters() && !$this->hasImages() && !$this->hasExtras()) 
    { 
     return [[ 
      'title' => 'test1', 
      'position0' => ['x0' => '165', 'x1' => '400', 'y0' => '265', 'y1' => '400'], 
      'position1' => ['x0' => '165', 'x1' => '405', 'y0' => '265', 'y1' => '405'], 
     ], 
     [ 
      'title' => 'test2', 
      'position0' => ['x0' => '295', 'x1' => '400', 'y0' => '395', 'y1' => '400'], 
      'position1' => ['x0' => '295', 'x1' => '405', 'y0' => '395', 'y1' => '405'], 
     ], 
     [ 
      'title' => 'test3', 
      'position0' => ['x0' => '425', 'x1' => '400', 'y0' => '525', 'y1' => '400'], 
      'position1' => ['x0' => '425', 'x1' => '405', 'y0' => '525', 'y1' => '405'], 
     ]]; 
    } 
} 

我试图创建的字符串(命令):

$itemCounter = 0; 
    $positionCounter = 0; 
    $recItemData = $this->getTextItems(); 
    foreach ($recItemData as $recItemDataKey => $recItemDataValue) 
    { 
     $rec = 'convert -size 720x480 xc:black -strokewidth 5 '; 
     $rec .= '-stroke lime '; 
     $rec .= '-draw "line ' . $recItemData[$itemCounter]['position'.$positionCounter]['x0'] . ',' . $recItemData[$itemCounter]['position'.$positionCounter]['y0']; 
     $rec .= ' ' . $recItemData[$itemCounter]['position'.$positionCounter]['x0'] . ',' . $recItemData[$itemCounter]['position'.$positionCounter]['y0'] . '"'; 
     if($itemCounter % count($recItemData) == 0) 
     { 
      $rec .= ' -stroke blue '; 
     } 
     $rec .= ' -draw "line ' . $recItemData[$itemCounter]['position'.$positionCounter]['x1'] . ',' . $recItemData[$itemCounter]['position'.$positionCounter]['y1']; 
     $rec .= ' ' . $recItemData[$itemCounter]['position'.$positionCounter]['x1'] . ',' . $recItemData[$itemCounter]['position'.$positionCounter]['y1'] . '"'; 
     if($positionCounter <= count($recItemData)) 
     { 
      $itemCounter++; 
     } 
    } 

目前,它打印的(这是错误的):

“转换 - 尺寸720×480 XC:黑色-strokewidth 5 -stroke石灰-draw “行425525 425525” -draw “行400,400 400,400” “

正确的输出应该是这样的:

转换 - 尺寸720×480 XC:黑色-strokewidth 5 -stroke石灰-draw”线 165,400 265 400" -draw‘行295400 395400’-draw‘行425400 525400’-stroke蓝-draw‘行165405 265405’-draw‘行295405 395405’-draw‘行425405 525405’

+0

你是通过你的循环重新初始化的'$ rec'每次的价值,当你做'$ REC =“转换-size 720×480 XC:黑色-strokewidth 5”;' –

+0

好,我明白了,所以我确实需要保存每个循环的值或你将如何完成这个 – utdev

+0

啊我想我得到了它谢谢 – utdev

回答

0

感谢PatrickQ I找到了解决方案。 问题是我在每个循环中重新初始化$rec的值。

我简单的做法是在循环之外声明变量$rec

$rec = 'convert -size 720x480 xc:black -strokewidth 5 '; 
foreach ($recItemData as $recItemDataKey => $recItemDataValue) 
{ 
    $rec .= '-stroke lime '; 
    $rec .= '-draw "line ' . $recItemData[$itemCounter]['position'.$positionCounter]['x0'] . ',' . $recItemData[$itemCounter]['position'.$positionCounter]['y0']; 
    $rec .= ' ' . $recItemData[$itemCounter]['position'.$positionCounter]['x0'] . ',' . $recItemData[$itemCounter]['position'.$positionCounter]['y0'] . '"'; 
    if($itemCounter % count($recItemData) == 0) 
    { 
     $rec .= ' -stroke blue '; 
    } 
    $rec .= ' -draw "line ' . $recItemData[$itemCounter]['position'.$positionCounter]['x1'] . ',' . $recItemData[$itemCounter]['position'.$positionCounter]['y1']; 
    $rec .= ' ' . $recItemData[$itemCounter]['position'.$positionCounter]['x1'] . ',' . $recItemData[$itemCounter]['position'.$positionCounter]['y1'] . '"'; 
    if($positionCounter <= count($recItemData)) 
    { 
     $itemCounter++; 
    } 
}