I have two lists which I have sorted with asort().

$weightings = Array([7] => 1,[3] => 2,[1] => 10,[2] => 10,[10] => 10,[6] => 10,[4] => 15,[5] => 15,[8] => 20,[9] => 20);

$revised_date = Array([1] => 1378652385,[2] => 1378654024,[3] => 1378654113,[4] => 1378654151,[5] => 1378654201,[6] => 1378654239,[7] => 1378654273,[8] => 1378841224,[9] => 1378842139,[10] => 1378842189);

a third list contains the titles of the articles that I want published on a website in order of their weighting, primarily and, if two or more articles have the same weighting, then the date that the item was revised.

$titles = Array([1]=>"Our location",[2]=>"Contact Details",[3]=>"Services we offer",[4]=>"Boats for sale",[5]=>"Cars for sale",[6]=>"Bikes for sale",[7]=>"News",[8]=>"History",[9]=>"Legends",[10]=>"How to find us...",);

The keys to all three lists refer to the same article.

Is there a simple way of acheiving this?

Yup, try with a loop and ksort, an example:

<?php

$weightings = array(7 => 1,3 => 2,1 => 10,2 => 10,10 => 10,6 => 10,4 => 15,5 => 15,8 => 20,9 => 20);
$revised_date = array(1 => 1378652385,2 => 1378654024,3 => 1378654113,4 => 1378654151,5 => 1378654201,6 => 1378654239,7 => 1378654273,8 => 1378841224,9 => 1378842139,10 => 1378842189);
$titles = array(1=>"Our location",2=>"Contact Details",3=>"Services we offer",4=>"Boats for sale",5=>"Cars for sale",6=>"Bikes for sale",7=>"News",8=>"History",9=>"Legends",10=>"How to find us...",);

$new = array();

foreach($weightings as $key => $value)
{
    $new[$weightings[$key]][$revised_date[$key]] = array(
        'id'     => $key,
        'title'  => $titles[$key]
    );

    ksort($new[$weightings[$key]], SORT_NUMERIC);
}

# uncomment below to order weightings in decrescent order
# asort($new, SORT_NUMERIC);

print_r($new);

It outputs:

Array
(
    [1] => Array
        (
            [1378654273] => Array
                (
                    [id] => 7
                    [title] => News
                )

        )

    [2] => Array
        (
            [1378654113] => Array
                (
                    [id] => 3
                    [title] => Services we offer
                )

        )

    [10] => Array
        (
            [1378652385] => Array
                (
                    [id] => 1
                    [title] => Our location
                )

            [1378654024] => Array
                (
                    [id] => 2
                    [title] => Contact Details
                )

            [1378654239] => Array
                (
                    [id] => 6
                    [title] => Bikes for sale
                )

            [1378842189] => Array
                (
                    [id] => 10
                    [title] => How to find us...
                )

        )

    [15] => Array
        (
            [1378654151] => Array
                (
                    [id] => 4
                    [title] => Boats for sale
                )

            [1378654201] => Array
                (
                    [id] => 5
                    [title] => Cars for sale
                )

        )

    [20] => Array
        (
            [1378841224] => Array
                (
                    [id] => 8
                    [title] => History
                )

            [1378842139] => Array
                (
                    [id] => 9
                    [title] => Legends
                )

        )

)

If you want to reverse the order of the dates simply use krsort(): http://www.php.net/manual/en/function.krsort.php

Thanks very much.

I was trying to loop through the arrays, but it was doing my head in.

Thanks again.

I couldn't get it to work.

In the end I tried this which is a little clunky, but it does appear to work.

<?php
$weightings = array(7 => 1,3 => 2,1 => 10,2 => 10,10 => 10,6 => 10,4 => 15,5 => 15,8 => 20,9 => 20);
$revised_date = array(1 => 1378652385,2 => 1378654024,3 => 1378654113,4 => 1378654151,5 => 1378654201,6 => 1378654239,7 => 1378654273,8 => 1378841224,9 => 1378842139,10 => 1378842189);
$titles = array(1=>"Our location",2=>"Contact Details",3=>"Services we offer",4=>"Boats for sale",5=>"Cars for sale",6=>"Bikes for sale",7=>"News",8=>"History",9=>"Legends",10=>"How to find us...",);
$new = array();
$dates = array();
$pval=-1;
foreach($weightings as $key => $value)
{
  if($pval==-1){
    $new[$key]=$titles[$key];
    $pval=$value;
  }
  else{
    if($pval==$value){
      $dates[$key]=$revised_date[$key];
        print_r($dates);
        echo "<hr>";
      unset($new[$key]);
    }
    else{
      if (count($dates)>0){
        print_r($dates);
        echo "<hr>";
        asort($dates);
        foreach($dates as $idkey => $value){
          $new[$idkey]=$titles[$idkey];
        }
        $dates=array();
      }
      $new[$key]=$titles[$key];
    }
    $pval=$value;
  }
}

?>

Your intent is to separate the groups by an <hr> tag?

Basing on my example, loop it this way:

foreach($new as $key => $value)
{
  foreach($value as $list => $article)
  {
    echo $article['title'] . PHP_EOL;
  }
  echo '<hr />'.PHP_EOL;
}

It will output:

News
<hr />
Services we offer
<hr />
Our location
Contact Details
Bikes for sale
How to find us...
<hr />
Boats for sale
Cars for sale
<hr />
History
Legends
<hr />

No, sorry, that was my debugging which I failed to take out.

Thanks again, I'll have another look at your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.