-1

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);
6
  • 2
    Try echo '<pre>',print_r($yourArray),'</pre>'; for a more readable output. Commented Sep 16, 2013 at 8:53
  • 1
    the problem with your array is not readable, could you please print as @Amal request above and post it then we can see it...clearly.. Commented Sep 16, 2013 at 8:55
  • 1
    best would be echo "<pre>";var_export($array);echo "</pre> Commented Sep 16, 2013 at 8:56
  • Can you please give a sample of the XML. Commented Sep 16, 2013 at 8:57
  • Check my answer below. I think that is exactly what you are looking for. First it sorts the multidimensional array based on the year value. Then it iterates over that sorted array inserting extra line break whenever it sees a change in year from previous row Commented Sep 16, 2013 at 10:34

2 Answers 2

0

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
Sign up to request clarification or add additional context in comments.

4 Comments

I would add to this just a var to remember the current year so you can put the extra line in something like if(!$current) $current = $year; if ($current != $year) { echo '<br/>'; $current = $year; }
i have not understood , what do you want to do with the current ,and where ?
Sorry, I threw in an edit into your post I think you can peer review it to take a look, if not, I'll try to explain more :)
i rejected your edit because your code is not good , i have not understood,explain me clearly what do you want to do ?
0

Try this solution

<?php
$arr[0] = array (2015,'A15');
$arr[1] = array (2014,'D14');
$arr[2] = array (2014,'E14');
$arr[3] = array (2013,'B13');
$arr[4] = array (2013,'C13');
$arr[5] = array (2012,'F12');


$data = array();
foreach($arr as $key => $val) {
    $data[$key]=$val[0];
}
array_multisort($data,SORT_ASC,$arr);

$check=$arr[0][0];
for($i=0;$i<count($arr);$i++) {
    if($arr[$i][0]!=$check) {
        $check=$arr[$i][0];
        print "<br/>";
    }
    print $arr[$i][0] . "---" . $arr[$i][1] . "<br/>";
}

?>

Output

2012---F12

2013---B13
2013---C13

2014---D14
2014---E14

2015---A15

5 Comments

Would you mind explaining a bit what you are doing here, please? Am I supposed to assign to an individual array every value I have on my year list?
This is the result I am looking for but I can't possibly re-assign every year value to a new array, there are like 70 of them.
@IreM you are not assigning all the values for each of the year list to the array, you are just assigning the year value. $data is just a one dimensional array containing all the year values only
@IreM I see your data is array is already sorted in the data you gave, then you need the code from for loop only
yes, I've realised that and tried to change the loop a bit and use it but it gets stuck.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.