Closed Thread Icon

Topic awaiting preservation: Help with arrays... Pages that link to <a href="https://ozoneasylum.com/backlink?for=31111" title="Pages that link to Topic awaiting preservation: Help with arrays..." rel="nofollow" >Topic awaiting preservation: Help with arrays...\

 
Author Thread
DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 07-09-2009 20:31

Good afternoon.
I have a project I am working on ( http://www.ozoneasylum.com/31110 ) for which I am using some javascript to display points on a map, and a popup on click.

In order to determine the points of the map, I am querying a database, building an array, checking various data points and assigning a "place" of 1-25 accordingly.
I then determine pixel coordinates based on the place.

This all works great.

What I need to do is determine if there is more than one item at any given "place", and offset the coordinates of the image accordingly.

The array can have any number of items (we're usually talking between maybe 15 and 30...not huge arrays), and each item has an array of various data points including the "place".
I get very confused with the various array functions in PHP, and I am a bit stumped as to how to approach this.

I basically want to be able to sort the array based on the "place" (values of anywhere from 1 - 25), and loop through. If the "place" value is the same as the previous item's place value, I will add an incremental offset to the coordinates.

I guess what I am stuck on is how to sort the array based on a value in a sub array...

Any tips on that task, or suggestions of better methods to achieve this are very welcome!


thanks

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 07-10-2009 11:25

you need to write a comparison function that's passed the arrays, looks at the sub arrays, and returns -1, 0 or 1 depending on larger, equal or smaller (or maybe it's the other way around - I never can remember).

Then you'd use either php->usort php->uasort or php->uksort depending on your needs .

So long,

->Tyberius Prime

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 07-13-2009 19:37

Ok, I am having a really tough time grasping what I need to actually do in the comparison function.
I'll give the manual a bit more reading on those sort functions and see what I come up with. I imagine I'll be back with more questions!!

thanks TP.

((for reference, this is an example of the array I will need to apply this to:

code:
Array
(
    [0] => Array
        (
            [ves_id] => 96
            [name] => VECCHIO BRIDGE
            [pol] => HKHKG
            [pod] => USNYC
            [etd] => 2009-06-07
            [eta] => 2009-07-01
            [place] => 25
        )

    [1] => Array
        (
            [ves_id] => 95
            [name] => HYUNDAI VOYAGER
            [pol] => TWKHH
            [pod] => USNYC
            [etd] => 2009-06-09
            [eta] => 2009-07-02
            [place] => 25
        )

    [2] => Array
        (
            [ves_id] => 99
            [name] => HANJIN VALENCIA
            [pol] => CNNGB
            [pod] => USNYC
            [etd] => 2009-06-14
            [eta] => 2009-07-08
            [place] => 25
        )

    [3] => Array
        (
            [ves_id] => 100
            [name] => VINCENT THOMAS BRIDGE
            [pol] => CNYTN
            [pod] => USNYC
            [etd] => 2009-06-15
            [eta] => 2009-07-08
            [place] => 25
        )

    [4] => Array
        (
            [ves_id] => 101
            [name] => ITAL LAGUNA
            [pol] => CNSHA
            [pod] => USNYC
            [etd] => 2009-06-16
            [eta] => 2009-07-13
            [place] => 25
        )

    [5] => Array
        (
            [ves_id] => 103
            [name] => VANCOUVER BRIDGE
            [pol] => CNYTN
            [pod] => USNYC
            [etd] => 2009-06-22
            [eta] => 2009-07-14
            [place] => 24
        )

    [6] => Array
        (
            [ves_id] => 105
            [name] => HANJIN MARSEILLES
            [pol] => CNSHA
            [pod] => USELZ
            [etd] => 2009-06-24
            [eta] => 2009-07-15
            [place] => 23
        )

    [7] => Array
        (
            [ves_id] => 106
            [name] => VERRAZANO BRIDGE
            [pol] => HKHKG
            [pod] => USNYC
            [etd] => 2009-06-29
            [eta] => 2009-07-22
            [place] => 15
        )

    [8] => Array
        (
            [ves_id] => 109
            [name] => HYUNDAI GOODWILL
            [pol] => TWKHH
            [pod] => USNYC
            [etd] => 2009-06-30
            [eta] => 2009-07-23
            [place] => 14
        )

    [9] => Array
        (
            [ves_id] => 104
            [name] => HANJIN DURBAN
            [pol] => TWKHH
            [pod] => USNYC
            [etd] => 2009-06-27
            [eta] => 2009-07-24
            [place] => 15
        )

    [10] => Array
        (
            [ves_id] => 114
            [name] => HANJIN PORTLAND
            [pol] => CNSHA
            [pod] => USEZA
            [etd] => 2009-07-06
            [eta] => 2009-07-29
            [place] => 8
        )

    [11] => Array
        (
            [ves_id] => 110
            [name] => VALENCIA BRIDGE
            [pol] => CNYTN
            [pod] => USNYC
            [etd] => 2009-07-06
            [eta] => 2009-07-29
            [place] => 8
        )

    [12] => Array
        (
            [ves_id] => 112
            [name] => YM TAICHUNG
            [pol] => TWKHH
            [pod] => USNYC
            [etd] => 2009-07-05
            [eta] => 2009-07-31
            [place] => 8
        )

    [13] => Array
        (
            [ves_id] => 115
            [name] => ITAL MILIONE
            [pol] => CNSHA
            [pod] => USNYC
            [etd] => 2009-07-07
            [eta] => 2009-08-03
            [place] => 6
        )

    [14] => Array
        (
            [ves_id] => 111
            [name] => OOCL OAKLAND
            [pol] => THBKK
            [pod] => USNYC
            [etd] => 2009-07-05
            [eta] => 2009-08-04
            [place] => 7
        )

    [15] => Array
        (
            [ves_id] => 116
            [name] => VIRGINIA BRIDGE
            [pol] => HKHKG
            [pod] => USNYC
            [etd] => 2009-07-12
            [eta] => 2009-08-05
            [place] => 1
        )

)



For my purposes at the moment, [place] is the only value that is relevant to the sort I need to do...

(Edited by DL-44 on 07-13-2009 19:39)

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 07-13-2009 20:55

Ok...I am just not getting it

I know I can be dense with these things...
But if you can give any further insight on this I would be very grateful.

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 07-14-2009 08:56
code:
myArray = ... the stuff above...

function sortByPlace(a, b)
{
 if (a["place"] > b["place"])
   return 1;
 else if (a["place"] < b["place"])
   return -1;
 else return 0;
}

function cleverSortByPlace(a,b)
{
 return a["place"] - b["place"];
}
myArray.sort(cleverSortByPlace);

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 07-14-2009 13:22

That's more or less what I was doing (based on the example in the manual), but without success.
Instead of a sorted array, all I get back is '1' after the usort()....



Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 07-14-2009 15:18

right... php, not javascript .

Usort sorts *in-place*, so you don't get anything sensible returned.

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 07-14-2009 17:24

Ok, well that just confuses me more

Right now, since I really need this to just work, and since it is a very small amount of data, I have grabbed this > http://firsttube.com/read/sorting-a-multi-dimensional-array-with-php/
which suits my immediate needs.

I will continue trying to grasp this aspect of programming while it continues to elude me though
Thanks for your help TP!

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 07-15-2009 09:22

too elaborate, sorting can (like every mutating operation, actually) either
a) return a new array
b) change the existing array. (change it 'in-place')

b) is what usort does.

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 07-15-2009 18:19

But...

If I usort the array, and then print_r the array....shouldn't I still see the array printed?

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 07-16-2009 10:22
quote:

That's more or less what I was doing (based on the example in the manual), but without success.
Instead of a sorted array, all I get back is '1' after the usort()....



Read to me as if you did

code:
$myArray = ...
print_r(usort($myArray));


when what you wanted was

code:
$myArray = ...
usort($myArray);
print_r($myArray);



but of coures I might have misunderstood you.

« BackwardsOnwards »

Show Forum Drop Down Menu