Php 如何在循环之外存储变量
我对循环外的变量有问题。我想存储下面显示的Php 如何在循环之外存储变量,php,Php,我对循环外的变量有问题。我想存储下面显示的foreach$time\u arr循环中的$programme\u title变量,并返回如下字符串的简短列表: Paid Programming The Mentalist - Black HeartsAnthony Bourdain Parts Unknown - Russia NFL Live StosselBad Dog! - Bad to the BoneHouse - Son of Coma GuyCutlery Corner Wild T
foreach$time\u arr
循环中的$programme\u title
变量,并返回如下字符串的简短列表:
Paid Programming The Mentalist - Black HeartsAnthony Bourdain Parts Unknown - Russia
NFL Live StosselBad Dog! - Bad to the BoneHouse - Son of Coma GuyCutlery Corner Wild
The Real Housewives of Atlanta - Secrets RevealedThe Real Housewives of Atlanta - Secrets Revealed
The Real Housewives of Atlanta - Secrets Revealed The Real Housewives of Atlanta - Secrets Revealed
The Real Housewives of Atlanta - Secrets Revealed The Real Housewives of Atlanta - Secrets Revealed
The Real Housewives of Atlanta - Secrets RevealedThe Real Housewives of Atlanta - Secrets Revealed
当我使用此代码时:
foreach($programme_arr as $programme)
{
$programme1 = $programme->item(0)->nodeValue;
$programme_title = $programme1;
}
echo $programme_title;
foreach($programme_arr as $programme)
{
$programme1 = $programme->item(0)->nodeValue;
$programme_title = $programme1;
echo $programme_title;
}
或者试试这个:
foreach($programme_arr as $programme)
{
$programme1 = $programme->item(0)->nodeValue;
$programme_title = $programme1;
}
foreach($time_arr as $time)
{
echo $programme_title;
}
它不起作用。因此,我尝试了以下代码:
foreach($programme_arr as $programme)
{
$programme1 = $programme->item(0)->nodeValue;
$programme_title = $programme1;
}
echo $programme_title;
foreach($programme_arr as $programme)
{
$programme1 = $programme->item(0)->nodeValue;
$programme_title = $programme1;
echo $programme_title;
}
并得到了一大串字符串:
Step Up 3The 700 Club The Fresh Prince of Bel-Air - Not With My Cousin You Don't
The Fresh Prince of Bel-Air - Viva Lost Wages The Fresh Prince of Bel-Air - There's the Rub
The Fresh Prince of Bel-Air - There's the RubSummer Sexy With T25!Dr. Ordon's Secret!
The 700 ClubAirbrushed BeautySleep Better!Joseph PrinceLife Today With James Robison - SMF Special 1
Joyce Meyer: Enjoying Everyday LifeShaun T's Focus T25That '70s Show - The Velvet Rope
That '70s Show - Laurie and the ProfessorThat '70s Show - HalloweenThat '70s Show - Van Stock
等等
您能告诉我如何将
$programme\u title
变量存储在foreach$time\u arr
中,以便打印如上所述的字符串列表吗?这是变量范围的问题。如果要在循环复杂时访问变量,则变量必须存在于循环外部。如果您这样做:
$programme_title = NULL;
foreach($programme_arr as $programme) {
$programme1 = $programme->item(0)->nodeValue;
$programme_title = $programme1;
}
echo $programme_title;
您将回显$programme\u arr
中最后一项的标题。如果要获取所有标题,需要将它们放入一个数组中:
$titles = array();
foreach($programme_arr as $programme) {
$programme1 = $programme->item(0)->nodeValue;
$titles[] = $programme1;
}
print_r($titles);
是的,我可以,但我尝试使用类似于
$program\u title=array($program->item(0)->NodeValue)的东西代码>我将获得输入,类似于ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray
。您知道如何将变量$programme\u title
与数组一起使用吗?您想将什么保存到$time\u arr
?$programme\u arr
的所有行?真的不清楚你想做什么。你对这个问题没有一点背景或更清楚的解释吗?@TomKriek是的,我想在$time\u arr循环下使用$program\u标题,因为我正在创建$xml变量,所以我可以将$program\u标题存储在$xml变量中。是的,我想得到$program\u arr的所有行,允许我在foreach$time\u arr下存储变量$program\u title。我怎么做?