Php trim string from array values -
i have function in class holds several arrays looks this:
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'], ]]; } }
i tried create string (command) this:
$itemcounter = 0; $positioncounter = 0; $recitemdata = $this->gettextitems(); foreach ($recitemdata $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++; } }
currently prints this(which wrong):
"convert -size 720x480 xc:black -strokewidth 5 -stroke lime -draw "line 425,525 425,525" -draw "line 400,400 400,400""
the correct output should this:
convert -size 720x480 xc:black -strokewidth 5 -stroke lime -draw "line 165,400 265,400" -draw "line 295,400 395,400" -draw "line 425,400 525,400" -stroke blue -draw "line 165,405 265,405" -draw "line 295,405 395,405" -draw "line 425,405 525,405"
thanks patrickq found solution. issue was re-initializing value of $rec
on each loop.
what simple did declare variable $rec
outside of loop.
$rec = 'convert -size 720x480 xc:black -strokewidth 5 '; foreach ($recitemdata $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++; } }
Comments
Post a Comment