Jump to content

Archived

This topic is now archived and is closed to further replies.

kirk112

Sorting Arrays

Recommended Posts

Where to start....

I have the following array

[code]

Array

(
    [0] => Array
        (
            [1] => Array
                (
                    [consutlant] => 1
                    [value] => 0
                    [fee] => 0
                )

            [2] => Array
                (
                    [consutlant] => 2
                    [value] => £1000
                    [fee] => 1000
                )

            [3] => Array
                (
                    [consutlant] => 3
                    [value] => £321
                    [fee] => 321
                )

            [4] => Array
                (
                    [consutlant] => 4
                    [value] => 0
                    [fee] => 0
                )
        )

    [1] => Array

        (
            [1] => Array
                (
                    [consutlant] => 1
                    [value] => 0
                    [fee] => 0
                )

            [2] => Array
                (
                    [consutlant] => 2
                    [value] => £1500
                    [fee] => 2500
                )

            [3] => Array
                (
                    [consutlant] => 3
                    [value] => 0
                    [fee] => 321
                )

            [4] => Array
                (
                    [consutlant] => 4
                    [value] => 0
                    [fee] => 0
                )
        )

    [2] => Array
        (
            [1] => Array
                (
                    [consutlant] => 1
                    [value] => 0
                    [fee] => 0
                )

            [2] => Array
                (
                    [consutlant] => 2
                    [value] => £3000
                    [fee] => 5500
                )

            [3] => Array

                (
                    [consutlant] => 3
                    [value] => 0
                    [fee] => 321
                )

            [4] => Array
                (
                    [consutlant] => 4
                    [value] => 0
                    [fee] => 0
                )

        )

[/code]


When I loop through the array and echo the results out into table rows, I get


[code=php:0]

Consultant 1   Consultant 2   Consultant 3   Consultant 4

       0          1000            321                 0

     500          1500             0                  0

       0          3000             0                 500

[/code]


What I am trying to figure out is how to sort the array so that does not echo the '0' values and also place the value as the first row in the table ie

[code=php:0]

Consultant 1   Consultant 2   Consultant 3   Consultant 4

      500         1000            321              500

                   1500                             

                   3000                             

[/code]


Thank you for your help


I have attached the full source code to make it easier to understand what I am tring to do


[attachment deleted by admin]

Share this post


Link to post
Share on other sites
i had to figure out how to sort arrays for my little mmorpg project, so i know where your coming from in the confusion, here's the part of my code that handles the sorting, see if you can learn from it maybe?

[code]
while ($sPC < $mPC) {
$order[$sPC] = read_key("PC", $sPC, "", "users.ini");
$orderL[$sPC] = read_key("INFO", "level", 0, read_key("PC", $sPC, "", "users.ini") . "MEEP.ini");
$sPC++;
}
array_multisort($order, SORT_NUMERIC, $orderL);
[/code]


for further reference:

http://us3.php.net/manual/en/function.array-multisort.php

and

http://us3.php.net/manual/en/ref.array.php





hope this helped - TL

Share this post


Link to post
Share on other sites
Thanks for the reply,

.... that has gone way over my head. 

I might be missing something but what should I set $sPc and mPC too?  also what does read_key do?

Sorry this has gone well past my level.

Thanks for your help really appreciate it!!

Share this post


Link to post
Share on other sites
*Bump

There more I think about it, I don't think I have to change the order of the array I have to rearrange values in the keys...

Any help would be really appricated,  spent soooooo long trying to figure this one out.

Share this post


Link to post
Share on other sites
[quote]What I am trying to figure out is how to sort the array so that does not echo the '0' values and also place the value as the first row in the table [/quote]

As your example only list value fields I am have difficulty understanding exactly in what sequence you want the output and what you mean by "value as the first row in the table"

Share this post


Link to post
Share on other sites
Hi Barand,

Did you download the example file?  Where it has the list of consultants under each consultant it has 10 values what I want is for it only to display the result if the value is not equal to 0 which is stright forward but I want all the value to move up the table so that there are not any empty cells

Hope this make sense

Share this post


Link to post
Share on other sites
Like this?
[code]
<?php
$data = array

(
    '0' => array
        (
            '1' => array
                (
                    'consultant' => 1,
                    'value' => 0,
                    'fee' => 0
                ),

            '2' => array
                (
                    'consultant' => 2,
                    'value' => 1000,
                    'fee' => 1000
                ),

            '3' => array
                (
                    'consultant' => 3,
                    'value' => 321,
                    'fee' => 321
                ),

            '4' => array
                (
                    'consultant' => 4,
                    'value' => 0,
                    'fee' => 0
                )
        ),

    '1' => array

        (
            '1' => array
                (
                    'consultant' => 1,
                    'value' => 500,
                    'fee' => 0
                ),

            '2' => array
                (
                    'consultant' => 2,
                    'value' => 1500,
                    'fee' => 2500
                ),

            '3' => array
                (
                    'consultant' => 3,
                    'value' => 0,
                    'fee' => 321
                ),

            '4' => array
                (
                    'consultant' => 4,
                    'value' => 0,
                    'fee' => 0
                )
        ),

    '2' => array
        (
            '1' => array
                (
                    'consultant' => 1,
                    'value' => 0,
                    'fee' => 0
                ),

            '2' => array
                (
                    'consultant' => 2,
                    'value' => 3000,
                    'fee' => 5500
                ),

            '3' => array

                (
                    'consultant' => 3,
                    'value' => 0,
                    'fee' => 321
                ),

            '4' => array
                (
                    'consultant' => 4,
                    'value' => 500,
                    'fee' => 0
                )

        )
);

// rearrange the array data

$values = array();
foreach ($data as $subarray) {
    foreach ($subarray as $cdata) {
        if ($cdata['value'])
            $values[$cdata['consultant']][] = $cdata['value'];
    }
}

// output the data

ksort($values) ;
$k = count($data);
echo '<table border="1">';
echo "<tr>";
// headings
foreach($values as $c => $v)
    echo "<td>Consultant $c</td>";
echo "</tr>";
// data
for ($i=0; $i<$k; $i++) {
    echo "<tr>";
    foreach($values as $c => $v) {
        echo "<td>";
        echo (isset($v[$i])) ? $v[$i] : '&nbsp';;
        echo "</td>";   
    }
    echo "</tr>";   
}
echo '</table>';

?>
[/code]

Share this post


Link to post
Share on other sites
Thank again Barnard

sorry I don't think I am explaining myself very well,  I have attached the file I am currently working on, from this you will see what the problem is - hopefully

the user_array and the total_res array are normally call from the database, I have edited them to make more sense

Thank again

Shaun

attached file commision.php

[attachment deleted by admin]

Share this post


Link to post
Share on other sites
Hi Barnard

The example that you used sorts out the array but since the original example array I have posted I have had to include and date value, and need to maintain the link between the consultant, value and date.  So as per the attachment I can sort the results grouped by month.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.