您好如何将echo $ row2放入每个$ row2的一个变量中?
每天$ row会有5个匹配,每个匹配3-6 $ ratt为$ row2。
如何将每个$ ratt [1]变为像
这样的变量$day1 = "...|...|...|...|...|...";
$day2 = "...|...|...|...|...|...";
$day3 = "...|...|...|...|...|...";
$day4 = "...|...|...|...|...|...";
$day5 = "...|...|...|...|...|...";
preg_match_all('/<div id=\"lunchmenyspaltrubrik\">(.*)<\/div>.*<ul>(.*)<\/ul>/Us', $contents, $dag, PREG_SET_ORDER);
foreach ($day as $row ) {
preg_match_all('/<div id=\"nyheterspaltbrod\">(.*?)<\/div>/', $row[2], $ratt);
foreach ($ratt[1] as $row2 ) {
echo $row2.'<br>';
}
}
答案 0 :(得分:0)
我建议将它们放在一个数组中: Live demo (click).
$days = [];
foreach ($ratt[1] as $row2 ) {
$days[] = $row2;
}
var_dump($days[0]);
var_dump($days[1]);
//etc
如果你真的想,你可以使用php的变量变量来获取个体变量: Live demo (click).
$myArr = $ratt[1];
for ($i=0; $i<count($myArr); ++$i) {
$v = $myArr[$i];
$varName = 'day'.$i;
$$varName = $v;
}
var_dump($day0);
var_dump($day1);
//etc
如果您希望从1
开始编号,而不是0
(这不推荐!),在编写变量名时只需+1 $i
:
$varName = 'day'.($i+1);