duanbei1598 2014-05-09 15:13
浏览 36
已采纳

如何在循环外存储变量

I have got a problem with the variable outside the loops. I want to store the variable of $programme_title from the foreach $time_arr loop shown below, and get back a short list of strings like this:

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

When I used this code:

 foreach($programme_arr as $programme)
  {
    $programme1 = $programme->item(0)->nodeValue;
    $programme_title = $programme1;
  }
  echo $programme_title;

Or try this:

foreach($programme_arr as $programme)
{
  $programme1 = $programme->item(0)->nodeValue;
  $programme_title = $programme1;
}


foreach($time_arr as $time)
{
   echo $programme_title;
}

It doesn't work. So, I tried this code:

foreach($programme_arr as $programme)
{
  $programme1 = $programme->item(0)->nodeValue;
  $programme_title = $programme1;
  echo $programme_title;
}

And got back this large list of string:

    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

and so on...

Can you please tell me how I can store the variable of $programme_title in the foreach $time_arr to allow me to print the list of strings like on above?

  • 写回答

1条回答 默认 最新

  • dousong2023 2014-05-09 15:28
    关注

    This is an issue with variable scope. Your variable has to exist outside the loop if you want to access it when the loop is comple. If you do this:

    $programme_title = NULL;
    foreach($programme_arr as $programme) {
        $programme1 = $programme->item(0)->nodeValue;
        $programme_title = $programme1;
    }
    
    echo $programme_title;
    

    You will echo the title of the last item in $programme_arr. If you want to get all the titles, need to put them in an array:

    $titles = array();
    foreach($programme_arr as $programme) {
        $programme1 = $programme->item(0)->nodeValue;
        $titles[] = $programme1;
    }
    
    print_r($titles);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画