kirk112 Posted October 24, 2006 Share Posted October 24, 2006 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 helpI have attached the full source code to make it easier to understand what I am tring to do[attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
True`Logic Posted October 24, 2006 Share Posted October 24, 2006 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.phpandhttp://us3.php.net/manual/en/ref.array.phphope this helped - TL Quote Link to comment Share on other sites More sharing options...
kirk112 Posted October 24, 2006 Author Share Posted October 24, 2006 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!! Quote Link to comment Share on other sites More sharing options...
kirk112 Posted October 24, 2006 Author Share Posted October 24, 2006 *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. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 24, 2006 Share Posted October 24, 2006 [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" Quote Link to comment Share on other sites More sharing options...
kirk112 Posted October 24, 2006 Author Share Posted October 24, 2006 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 cellsHope this make sense Quote Link to comment Share on other sites More sharing options...
Barand Posted October 24, 2006 Share Posted October 24, 2006 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 dataksort($values) ;$k = count($data);echo '<table border="1">';echo "<tr>";// headingsforeach($values as $c => $v) echo "<td>Consultant $c</td>";echo "</tr>";// datafor ($i=0; $i<$k; $i++) { echo "<tr>"; foreach($values as $c => $v) { echo "<td>"; echo (isset($v[$i])) ? $v[$i] : ' ';; echo "</td>"; } echo "</tr>"; }echo '</table>';?>[/code] Quote Link to comment Share on other sites More sharing options...
kirk112 Posted October 24, 2006 Author Share Posted October 24, 2006 Thank again Barnardsorry 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 - hopefullythe user_array and the total_res array are normally call from the database, I have edited them to make more senseThank againShaunattached file commision.php[attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
kirk112 Posted October 25, 2006 Author Share Posted October 25, 2006 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. Quote Link to comment Share on other sites More sharing options...
kirk112 Posted October 25, 2006 Author Share Posted October 25, 2006 *bump Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.