2010-03-22 137 views
-1

我需要一些帮助,数组和foreach循环,PHP数组和foreach帮助

我有一个数组,它是从数据库返回的数据,基本上是与之相关联的标题和图像的列表,它看起来有点像这样,

Array 
(
    [0] => Array 
     (
      [contentImageId] => 28 
      [contentImageName] => yellow.png 
      [contentImageType] => .png 
      [contentImagePath] => /var/www/bangmarketing.bang/media/uploads/contentImages/7/yellow.png 
      [isHeadlineImage] => 1 
      [contentImageDateUploaded] => 1265388055 
      [contentId] => 7 
      [dashboardUserId] => 0 
      [contentTitle] => Another Blog 
      [contentAbstract] => <p>This is another blog and it is shit becuase this does not work</p> 
      [contentBody] => <p>ioasfihfududfhdufhuishdfiudshfiudhsfiuhdsiufhusdhfuids</p> 
      [contentOnline] => 1 
      [contentAllowComments] => 0 
      [contentDateCreated] => 1265388034 
      [categoryId] => 1 
      [categoryTitle] => blogsss 
      [categoryAbstract] => <p>asdsdsadasdsadfdsgdgdsgdsgssssssssssss</p> 
      [categorySlug] => blog 
      [categoryIsSpecial] => 0 
      [categoryOnline] => 1 
      [categoryDateCreated] => 1266588327 
     ) 

    [1] => Array 
     (
      [contentImageId] => 31 
      [contentImageName] => sophie.jpg 
      [contentImageType] => .jpg 
      [contentImagePath] => /var/www/bangmarketing.bang/media/uploads/contentImages/9/sophie.jpg 
      [isHeadlineImage] => 1 
      [contentImageDateUploaded] => 1265713423 
      [contentId] => 9 
      [dashboardUserId] => 0 
      [contentTitle] => Bang appoints a new Senior Designer 
      [contentAbstract] => <p>Bang has appointed a new Senior Designer in the wake of a number of major client wins.</p> 
      [contentBody] => <p>After recently signing up clients including the National Trust and Propaganda, Bang has hired Sophie Smith to join its creative team.</p> 

<p>Sophie brings over twelve years of extensive design experience in Bang's core market sectors, including social housing and public sector brands.</p> 
<p>Sophie has significant experience of branding and strategic design projects for a broad range of clients, including HBoS, Harvey's, TKMaxx and Places for People.</p> 
<p>Warren Watts Creative Director at Bang commented; "Sophie has already proven an invaluable addition to our growing studio. In only her second week, she helped produce the creative that led to the National Trust account win".</p> 
      [contentOnline] => 1 
      [contentAllowComments] => 0 
      [contentDateCreated] => 1265713155 
      [categoryId] => 8 
      [categoryTitle] => News 
      [categoryAbstract] => <p>The world at Bang Marketing moves fast, keep up to date w 
      [categorySlug] => news 
      [categoryIsSpecial] => 0 
      [categoryOnline] => 1 
      [categoryDateCreated] => 1265283717 
     ) 

    [2] => Array 
     (
      [contentImageId] => 32 
      [contentImageName] => selectadna.jpg 
      [contentImageType] => .jpg 
      [contentImagePath] => /var/www/bangmarketing.bang/media/uploads/contentImages/10/selectadna.jpg 
      [isHeadlineImage] => 1 
      [contentImageDateUploaded] => 1265713937 
      [contentId] => 10 
      [dashboardUserId] => 0 
      [contentTitle] => Bang client experiences growth 
      [contentAbstract] => <p>Selectamark, the UK's leading asset protection specialist recorded its best figures for five years in 2008.</p> 
      [contentBody] => <p>The results were released during an extremely challenging economic climate, and just over 12 months into the organisation's relationship with Bang.</p> 
<p>Bang has been working on developing Selectamark's range of product brands, which include the UK's leading asset tracking and labelling system Selectamark, and second generation forensic marking system, SelectaDNA. Despite gloomy national economic forecasts, Selectamark has enjoyed an extremely successful start to 2009 &ndash; picking up several major new clients in February, and with discussions taking place to expand operations beyond the UK and Netherlands to Germany and New Zealand.</p> 

<p>Chris Garratt, Marketing Director at Bang Marketing Ltd explained "Selectamark adopted a well planned, coordinated and consistent strategy in 2008, and the results speak for themselves.</p> 
<p>"Due to the success last year and the forecasts for increased levels of acquisitive crime we're hoping Selectamark's key products will enjoy similar growth in 2009".</p> 
<p>Selectamark Security Systems provides DNA Forensic Marking products (SelectaDNA) and Visible Marking solutions (Selectamark and Logomark) to prevent burglaries, theft from void properties, theft of boilers, and theft of valuable metals such as lead from roofs. Selectamark have supplied these Secured By Design crime prevention products to Police, local authorities and housing associations for 25 years.</p> 
<p>The Selectamark system has now marked over 25 million items, and is patented and distributed world-wide. Visit www.selectadna.co.uk for product details.</p> 
<p>James Brown, Sales Director at Selectamark said "We have been absolutely delighted with the work Bang has done for us. In a short space of time, the Bang team has created a wide range of extremely fresh and professional-looking sales and marketing material for us to use, as well as devising a detailed marketing strategy which has now been implemented. We would have no hesitation in recommending Bang to other organisations."</p> 
      [contentOnline] => 1 
      [contentAllowComments] => 0 
      [contentDateCreated] => 1265713888 
      [categoryId] => 8 
      [categoryTitle] => News 
      [categoryAbstract] => <p>The world at Bang Marketing moves fast, keep up to date w 
      [categorySlug] => news 
      [categoryIsSpecial] => 0 
      [categoryOnline] => 1 
      [categoryDateCreated] => 1265283717 
     ) 
) 

正如你可以看到contentTitle变化,每次但有时是返回相同的内容标题,我正在寻找一种方法来添加返回的数据之间的鸿沟,如果内容标题价值变化?

这可能吗?如何?我真的在这里挣扎。

感谢

+0

你是什么意思的鸿沟? – Kevin 2010-03-22 17:10:18

+0

@kevin我认为hes在讨论何时在输出html的同时循环访问数组......他希望在标题更改时添加像hr或其他东西。 – prodigitalson 2010-03-22 17:13:56

回答

2

如您遍历,你可以有一个临时变量来储存最后显示的标题

$lastTitle=''; 
foreach ($array as $record) 
{ 
    if ($lastTitle != $record['contentTitle']) 
    echo '<hr>'; 

    $lastTitle = $record['contentTitle'); 

    // display content from record 

} 
1

如果我理解正确的话,你希望每次contentTitle是显示分频器不同于它在上次迭代中的价值。
你在这里做一种画廊,是吗? 如果是这样,我会这样想:

$lastTitle = ''; 
foreach($yourArray as $value) 
{ 
    if(!$lastTitle || $lastTitle != $value['contentTitle']) 
    { 
     echo '<h2>' . $value['contentTitle'] . '</h2><hr />'; // Display divider 
     // Do some other stuff 
    } 

    $lastTitle = $value['contentTitle']; 
    // Display image or whatever you want to do 
}