Forum Moderators: coopster

Message Too Old, No Replies

Sorting an array_merge()?

How to sort an array_merge()?

         

allyse

12:57 pm on Jun 4, 2003 (gmt 0)

10+ Year Member



Hi! I'm a PHP newbie and wonder how to sort a array_merge() after a date variable. I'm told to try bubble sort but I have no idea how to do it.

I got told to base me on this:
********************

Bubble sort (exampel written in c, but can "easyly" be translated to php):
for (i=0; i<n-1; i++) {
for (j=0; j<n-1-i; j++)
if (a[j+1] < a[j]) { /* compare the two neighbors */
tmp = a[j]; /* swap a[j] and a[j+1] */
a[j] = a[j+1];
a[j+1] = tmp;
}
}

**************

<snip>

The if-sentense must here be written too:
if($a[$j+1]->publisertLang < $a[$j]->publisertLang)
where a is $ingresser and $n is the number of elements in $ingresser.

(publisertLan is the date variable I want to sort the array with)

*************

The code I base on is:

$i1 = hentIngresser($nettstedid,38,$sprakid,$offsetannet,$antallmotor);
$i2 = hentIngresser($nettstedid,39,$sprakid,$offsetannet,$antallmotor);

if($ingresser = array_merge($i1 ,$i2))

listIngresser($ingresser);

else
echo 'Det oppstod en feil ved henting av ingresser.';

This is how the feed looks like: <snip>

Hope somebody can help me!

Regards,
A. Lyse

[edited by: jatar_k at 3:22 pm (utc) on June 4, 2003]
[edit reason] no specific urls thanks [/edit]

jatar_k

3:28 pm on Jun 4, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welocome to WebmasterWorld allyse,

There are lots of predefined array sorting functions. One that might work is asort [php.net]. There are others referenced from that page as well.

Since I am not sure of the data you are dealing with or the methodology it is difficult to say if this is the best answer though.

allyse

3:53 pm on Jun 4, 2003 (gmt 0)

10+ Year Member



I didn't think the predefined function did work with my use.

I can give you a more exact description:

********* main PHP file for website *************
<?php

include "../lib/ingress.lib";

$nettstedid=11;
$sprakid=1;
$offset=0;
$offset3ingr=2;
$offsetannet=1;
$antall=50;
$antallmotor=25;
$antallmotor3ingresser=3;
$antallmc=1;
$antallmc3ingresser=3;
$antallstyling=1;
$antallstyling3ingresser=3;
$typeid=0;
$i1 = hentIngresser($nettstedid,38,$sprakid,$offsetannet,$antallmotor);
$i2 = hentIngresser($nettstedid,39,$sprakid,$offsetannet,$antallmotor);


if($ingresser = array_merge($i1 ,$i2))

listIngresser($ingresser);

else
echo 'Det oppstod en feil ved henting av ingresser.';

function listIngresser($ingresser){
foreach($ingresser as $i){

echo the tables
}
}

?>

******** END MAIN PHP FILE ************

<modnote: way too much code see Charter [webmasterworld.com]>

I'll contineu on a new post as this one is getting long :)

Regards,
A. Lyse

[edited by: jatar_k at 4:10 pm (utc) on June 4, 2003]

allyse

4:00 pm on Jun 4, 2003 (gmt 0)

10+ Year Member



What I want to do:

I want to add to the Main PHP file, at this array (you can see the whole thing in the post above):

******** Code cuttet from the main PHP file *********

$i1 = hentIngresser($nettstedid,38,$sprakid,$offsetannet,$antallmotor);
$i2 = hentIngresser($nettstedid,39,$sprakid,$offsetannet,$antallmotor);

if($ingresser = array_merge($i1 ,$i2))

listIngresser($ingresser);

else
echo 'Det oppstod en feil ved henting av ingresser.';

******** END Code cuttet from the main PHP file *********

I want the "if($ingresser = array_merge($i1 ,$i2))" to sort the articles they gives regardless of the section (as $i1 and $i2 are two separate sections)

Now the system sorts first the $i1 and then $i2 come on.

I want to use the variable "publisertLang" ($publisertLang) you can see in the last result past in the above post as sorting thingy. The publisertLang is a "long date".

The admin says I can use the Buble Sort to do this, but I dont know how...

I'm a newbie in this and would love some help :) Please tell me if it is still unclear.

Regards,
A. Lyse

jatar_k

4:19 pm on Jun 4, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sorry allyse but, as per the charter, that is way too much code. It is also very difficult to read because I can't read any of the varnames, error messages or comments, norwegian not being one of my strong points.

From what i can see you have a multidimensional array that you need to sort?
have you looked at array_multisort [php.net]?