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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?