我想在foreach循环i.e. from [color] => 'grey' to [color] => 'green'
的最后一次迭代中动态替换某些键的值。
这是实际的脚本
<?php
$line = "After six months, participants in both vitamin D supplementation groups had lost more weight and had greater reductions in their waistlines than those who hadn't taken the supplements, Vigna's team said";
// $chunks = str_split($line, 35);
$array = explode("\n", wordwrap($line, 40, "\n"));
// echo '<pre>' . print_r($chunks, true);
function splitTextString($array)
{
foreach($array as $key=>$value)
{
$linesArr{$key}['name'] = $value;
$linesArr{$key}['font-size'] = 27;
$linesArr{$key}['color'] = "grey";
}
return $linesArr;
}
echo "<pre>";
print_r(splitTextString($array));
echo "</pre>";
?>
答案 0 :(得分:0)
只需更改上一次迭代中的字符串
<?php
$line = "After six months ...";
$array = explode("\n", wordwrap($line, 40, "\n"));
function splitTextString($array) {
end($array);
$last = key($array);
reset($array);
foreach($array as $key=>$value) {
$color = $key === $last ? "green" : "grey";
$linesArr{$key}['name'] = $value;
$linesArr{$key}['font-size'] = 27;
$linesArr{$key}['color'] = $color;
}
return $linesArr;
}
echo "<pre>";
print_r(splitTextString($array));
echo "</pre>";
?>