Php从数组中创建正确的字符串

Php从数组中创建正确的字符串,php,arrays,string,foreach,trim,Php,Arrays,String,Foreach,Trim,我正在尝试创建以下字符串: 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

我正在尝试创建以下字符串:

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"
从该数组数据(函数):

到目前为止,我做到了:

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

我做错了什么。

了解foreach的工作原理
$reademdatavalue
等于
$reademdatavalue
$itemCounter
等于
$reademdatakey
,重写它将产生更可读的代码

<?php

error_reporting(E_ALL);
ini_set('display_errors',1);
/**

*/
function getTextItems()
{

        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'],
                ]];

}

$recItemData = getTextItems();
$rec = 'convert -size 720x480 xc:black -strokewidth 5 ';
$size = count($recItemData)-1;
    $rec .= ' -stroke lime ';
foreach ($recItemData as $recItemDataKey => $recItemDataValue) {
    $rec .= ' -draw "line ' . $recItemDataValue['position0']['x0'] . ',' . $recItemDataValue['position0']['x1'];
    $rec .= ' ' . $recItemDataValue['position0']['y0'] . ',' . $recItemDataValue['position0']['y1'] . '"';
}
    $rec .= ' -stroke blue ';
foreach ($recItemData as $recItemDataKey => $recItemDataValue)
    {
    $rec .= ' -draw "line ' . $recItemDataValue['position1']['x0'] . ',' . $recItemDataValue['position1']['x1'];
    $rec .= ' ' . $recItemDataValue['position1']['y0'] . ',' . $recItemDataValue['position1']['y1'] . '"';
}

$desired = '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"';
echo $desired,"\n";
echo $rec,"\n";
试试这个:

$command = 'convert -size 720x480 xc:black -strokewidth 5';

$position0 = '-stroke lime';
$position1 = '-stroke blue';

foreach($this->getTextItems() as $v)
{
    $position0.= ' -draw "line '.$v['position0']['x0'].','.$v['position0']['x1'].' '.$v['position0']['y0'].','.$v['position0']['y1'].'"';

    $position1.= ' -draw "line '.$v['position1']['x0'].','.$v['position1']['x1'].' '.$v['position1']['y0'].','.$v['position1']['y1'].'"';
}

$command.= ' '.$position0.' '.$position1;

echo $command;

getTextItems()
是否总是返回这样的静态值列表?是否会有
位置2
位置3
,等等?@MonkeyZeus不会有位置2或位置3。。。。它总是返回值它有一些我没有显示的其他分支谢谢,但是我没有得到所需的字符串你得到正确的字符串了吗?它创建了我这样一个:
convert-size 720x480 xc:black-strokewidth 5-stroke lime-draw“line 165400 265400”-stroke blue-draw“line 165405 265405”-stroke lime-draw“line 295400 395400”-划蓝色-画“线295405 395405”-划石灰-画“线425400 525400”-划蓝色-画“线425405 525405”
我选择了蒙基宙斯的答案,因为我认为代码稍微好一点,不过我给了你一个“大拇指”,谢谢你的支持help@JohnDoe2我认为这很有效,但你必须补充一点“后面的线-draw@JohnDoe2我明白了,我把它修好了
<?php

error_reporting(E_ALL);
ini_set('display_errors',1);
/**

*/
function getTextItems()
{

        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'],
                ]];

}

$recItemData = getTextItems();
$rec = 'convert -size 720x480 xc:black -strokewidth 5 ';
$size = count($recItemData)-1;
    $rec .= ' -stroke lime ';
foreach ($recItemData as $recItemDataKey => $recItemDataValue) {
    $rec .= ' -draw "line ' . $recItemDataValue['position0']['x0'] . ',' . $recItemDataValue['position0']['x1'];
    $rec .= ' ' . $recItemDataValue['position0']['y0'] . ',' . $recItemDataValue['position0']['y1'] . '"';
}
    $rec .= ' -stroke blue ';
foreach ($recItemData as $recItemDataKey => $recItemDataValue)
    {
    $rec .= ' -draw "line ' . $recItemDataValue['position1']['x0'] . ',' . $recItemDataValue['position1']['x1'];
    $rec .= ' ' . $recItemDataValue['position1']['y0'] . ',' . $recItemDataValue['position1']['y1'] . '"';
}

$desired = '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"';
echo $desired,"\n";
echo $rec,"\n";
$command = 'convert -size 720x480 xc:black -strokewidth 5';

$position0 = '-stroke lime';
$position1 = '-stroke blue';

foreach($this->getTextItems() as $v)
{
    $position0.= ' -draw "line '.$v['position0']['x0'].','.$v['position0']['x1'].' '.$v['position0']['y0'].','.$v['position0']['y1'].'"';

    $position1.= ' -draw "line '.$v['position1']['x0'].','.$v['position1']['x1'].' '.$v['position1']['y0'].','.$v['position1']['y1'].'"';
}

$command.= ' '.$position0.' '.$position1;

echo $command;