Jump to content


Photo

Sorting Arrays


  • Please log in to reply
9 replies to this topic

#1 kirk112

kirk112
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts
  • LocationEngland

Posted 24 October 2006 - 01:11 PM

Where to start....

I have the following array


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
                )

        )



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



Consultant 1   Consultant 2   Consultant 3   Consultant 4

       0          1000            321                 0

     500          1500             0                  0

       0          3000             0                 500



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


Consultant 1   Consultant 2   Consultant 3   Consultant 4

      500         1000            321              500

                   1500                              

                   3000                              



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]

#2 True`Logic

True`Logic
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 24 October 2006 - 03:42 PM

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?

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);


for further reference:

http://us3.php.net/m...y-multisort.php

and

http://us3.php.net/m...n/ref.array.php





hope this helped - TL

#3 kirk112

kirk112
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts
  • LocationEngland

Posted 24 October 2006 - 04:05 PM

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!!

#4 kirk112

kirk112
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts
  • LocationEngland

Posted 24 October 2006 - 07:54 PM

*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.

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 24 October 2006 - 08:14 PM

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


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"
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 kirk112

kirk112
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts
  • LocationEngland

Posted 24 October 2006 - 08:43 PM

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

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 24 October 2006 - 10:27 PM

Like this?
<?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>';

?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 kirk112

kirk112
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts
  • LocationEngland

Posted 24 October 2006 - 10:56 PM

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]

#9 kirk112

kirk112
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts
  • LocationEngland

Posted 25 October 2006 - 06:20 AM

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.

#10 kirk112

kirk112
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts
  • LocationEngland

Posted 25 October 2006 - 07:36 PM

*bump




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users