donglei1616 2013-09-16 08:52
浏览 39

如何从数组中获取特定值而不删除它们的重复项? [重复]

This question already has an answer here:

I have an XML array with a list of page titles. I extracted the date part from the title and got an array going like:

Array ( [0] => 2012 ) 
Array ( [0] => 2013 ) 
Array ( [0] => 2013 )
Array ( [0] => 2014 ) 
Array ( [0] => 2014 ) 
Array ( [0] => 2014 ) 
Array ( [0] => 2015 )

My question is how can I loop through the date array so that I get an empty line between every new year, like:

  2012 - BLA

  2013 - BLA BLA
  2013 - BLABLA

  2014- BLA BLA BLA
  2014- BLA BLA BLA
  2014- BLA BLA BLA

  2015- BLA

What I thought is extracting the date part from the title and then going through the array and isnert a new line for every new unique value. Looking around I couldn't find a way of doing that without removing the duplicate values. Is there a way to do it? Please, I'd appreciate any help.

Regards

XML sample:

SimpleXMLElement Object ( [@attributes] => Array ( [version] => 2.0 ) [channel] => SimpleXMLElement Object ( [title] => Results [description] => Search Results [link] => http://...... [item] => Array ( [0] => SimpleXMLElement Object ( [title] => 2012 Blah , Blah Blah, Blah [description] => <b>Introduction</b> ...Long text.. [link] => http... [1] => SimpleXMLElement Object ( [title] 
...

What I've done is took the $year part out with :

$year = substr(str_replace($type.", ", "", $xmlfeed->channel->item[$i]->title),0,4);
</div>
  • 写回答

2条回答 默认 最新

  • douhutongvm382381 2013-09-16 09:04
    关注

    If your array contains arrays as it's in your question , then i suggest doing this : DEMO HERE:

    foreach($arrayOfArrays as $array){
    
         $simpleArray[]= $array[0];//simple array that contains only years
    }
    
    sort($simpleArray, SORT_NUMERIC); //we sort this array by year
    
    foreach($simpleArray as $year){
        echo $year.' Bla Bla Bla <br/>';//display every year in a line
    }
    

    OUTPUT:

    2011 Bla bla bla
    2012 Bla bla bla
    2013 Bla bla bla
    2013 Bla bla bla
    2013 Bla bla bla
    2015 Bla bla bla
    
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择