gdeliana Posted November 6, 2012 Share Posted November 6, 2012 (edited) Hello!!!! I have a while loop which outputs variables made from mysql query, but i want to order this output according to one of those php variables created later after the mysql query. I think that i can put all the outputted variables of the loop in an array and than order them in the array and output them. How can i make an array that will look like a matrix where nr.rows=nr.loops and nr.columns=nr.variables THAN order these rows by one column....like in excel: you create a table and than you sort them according to a column . After i output the array with a "for" loop for each row . PLS HEEELP. I know what i want but i don't know how can it be done EDIT: If your answers will be you can do it in mysql query.....than i say simply it's impossible......because the variable which i want to order the results from is generated AFTER the query and it's a mix of mysql variables($row[''])+form input variables ($_POST['']). So simply i cannot do it in mysql unless you suggest s.m.th else Edited November 6, 2012 by gdeliana Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/ Share on other sites More sharing options...
Muddy_Funster Posted November 6, 2012 Share Posted November 6, 2012 There are two choices as I see it, Query the database again and get the results in the order you want them learn about php arrays and the use of usort() Option 2 fits what you are asking, but we will need to see some code from you to see where you have got to so far before we can really help more. Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390482 Share on other sites More sharing options...
gdeliana Posted November 6, 2012 Author Share Posted November 6, 2012 Thanks for the reply . It's basically a financial claculator for savings. I have a list of saving companies in mysql which have their own account limitations. The calculations are done based in user form input and limited by the variables of mysql query. The output consists of divs where each div represents one saving company and for it i calculate the saving amount, tax, and the PROFIT. I have done all of this and the results output according to $row['interest_rate'], but i want them ordered by the later generated variable which is the $profit. I have already posted here my code before but as it was a bit long nobody wanted to look at it . A loop creates 6 php variable. So i want an array which will have in each row a loop with these 6 variables so 6 columns. It will look like a matrix. Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390487 Share on other sites More sharing options...
gdeliana Posted November 6, 2012 Author Share Posted November 6, 2012 (edited) If someone could tell me how to create such an array like a matrix. I know all the types of arrays (saw them in w3schools) but it seems that they just put the variables one after another and index them, one cannot know where a loop starts and ends. Edited November 6, 2012 by gdeliana Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390488 Share on other sites More sharing options...
Muddy_Funster Posted November 6, 2012 Share Posted November 6, 2012 you're looking to do something like $i = 0; while($row = mysql_fetch_assoc($result)){ $rArray[$i][$row['filedName1']] = $row['fieldName1']; $rArray[$i][$row['filedName2']] = $row['fieldName2']; $rArray[$i][$row['filedName3']] = $row['fieldName3']; $rArray[$i][$row['filedName4']] = $row['fieldName4']; $rArray[$i][$row['filedName5']] = $row['fieldName5']; $rArray[$i][$row['filedName6']] = $row['fieldName6']; $i++; //your current code here } Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390496 Share on other sites More sharing options...
gdeliana Posted November 6, 2012 Author Share Posted November 6, 2012 Thanks, i will try it. Is this a multidimensional array? And the name of the array is rArray? Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390499 Share on other sites More sharing options...
Muddy_Funster Posted November 6, 2012 Share Posted November 6, 2012 yes it is and yes it is. you can call it whatever you like though. Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390502 Share on other sites More sharing options...
Barand Posted November 6, 2012 Share Posted November 6, 2012 or $rArray = array(); while ($row = mysql_fetch_assoc($result)) { $rArray[] = $row; } Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390503 Share on other sites More sharing options...
gdeliana Posted November 6, 2012 Author Share Posted November 6, 2012 (edited) Ok, i tried it, but when i echo the results: echo "$rArray[$i][$row['filedName6']]"; i get only the result for that loop, i think the values are not pushed. Also noticed that you use the "mysql_fetch_assoc" and i use "mysql_fetch_array". Edited November 6, 2012 by gdeliana Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390506 Share on other sites More sharing options...
Muddy_Funster Posted November 6, 2012 Share Posted November 6, 2012 yeah, mysql_fetch_assoc() returns one dataset, where the keys are associative names from the table. mysql_fetch_array() returns two datasets, one with associative key names and one with numerical. By associative key names I meen that the array key for the value is the same as the column heading in the database. Rather than echoing, do var_dump($rArray); when you echo "$rArray[$i][$row['filedName6']]"; you will only display the value for the array record at the point of [$i] (which will be set to the last value in the array. If you want to echo each line on its own you will need to step through the array using a loop (as it's multi-dimensional you will need to nest two loops, one inside the other) something like the following for($counter = 0; $counter <= count($rArray)-1; $counter++){ foreach($rArray[$counter] as $key => $value){ echo "\$rArray --> record no $counter --> record key = $key --> record value = $value <br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390528 Share on other sites More sharing options...
gdeliana Posted November 7, 2012 Author Share Posted November 7, 2012 Ok i did it, however i need to sort the arrays, but not the values inside them. Here is the output: [b]array[/b] [i](size=13)[/i] 1 => [b]array[/b] [i](size=6)[/i] 54954 => float 54954.74347101 13125 => float 13125.628562903 1000374 => float 1000374.6160332 10097 => float 10097.841093294 11394 => float 11394.765048752 12575 => float 12575.18524806 2 => [b]array[/b] [i](size=6)[/i] 54954 => float 54954.74347101 13125 => float 13125.628562903 1000374 => float 1000374.6160332 10097 => float 10097.841093294 11394 => float 11394.765048752 12575 => float 12575.18524806 3 => [b]array[/b] [i](size=6)[/i] 54954 => float 54954.74347101 13125 => float 13125.628562903 1000374 => float 1000374.6160332 10097 => float 10097.841093294 11394 => float 11394.765048752 12575 => float 12575.18524806 4 => [b]array[/b] [i](size=6)[/i] 54954 => float 54954.74347101 13125 => float 13125.628562903 1000374 => float 1000374.6160332 10097 => float 10097.841093294 11394 => float 11394.765048752 12575 => float 12575.18524806 5 => [b]array[/b] [i](size=6)[/i] 49710 => float 49710.953291902 13198 => float 13198.458982057 1000734 => float 1000734.5436461 9178 => float 9178.6376459356 11261 => float 11261.62419264 12521 => float 12521.708395092 6 => [b]array[/b] [i](size=6)[/i] 49710 => float 49710.953291902 13198 => float 13198.458982057 1000734 => float 1000734.5436461 9178 => float 9178.6376459356 11261 => float 11261.62419264 12521 => float 12521.708395092 7 => [b]array[/b] [i](size=6)[/i] 49674 => float 49674.293645881 13198 => float 13198.968143807 1000773 => float 1000773.1493464 9178 => float 9178.991733577 11261 => float 11261.62419264 12521 => float 12521.708395092 8 => [b]array[/b] [i](size=6)[/i] 52384 => float 52384.439029542 13161 => float 13161.327235701 997919 => float 997919.13759795 9152 => float 9152.8150218379 8446 => float 8446.21814448 12521 => float 12521.708395092 9 => [b]array[/b] [i](size=6)[/i] 49772 => float 49772.052701936 13197 => float 13197.61037914 1000670 => float 1000670.2008123 9178 => float 9178.047499867 11261 => float 11261.62419264 12521 => float 12521.708395092 10 => [b]array[/b] [i](size=6)[/i] 49772 => float 49772.052701936 13197 => float 13197.61037914 1000670 => float 1000670.2008123 9178 => float 9178.047499867 11261 => float 11261.62419264 12521 => float 12521.708395092 11 => [b]array[/b] [i](size=6)[/i] 49772 => float 49772.052701936 13197 => float 13197.61037914 1000670 => float 1000670.2008123 9178 => float 9178.047499867 11261 => float 11261.62419264 12521 => float 12521.708395092 12 => [b]array[/b] [i](size=6)[/i] 25214 => float 25214.225740612 13538 => float 13538.691309158 1000320 => float 1000320.7701461 4575 => float 4575.8889235416 10615 => float 10615.20150601 12257 => float 12257.908486479 13 => [b]array[/b] [i](size=6)[/i] 25250 => float 25250.555281567 13538 => float 13538.1867322 1000283 => float 1000283.4889348 4575 => float 4575.7183835641 10615 => float 10615.20150601 12257 => float 12257.908486479 I need to sort them by the first value, but maintain the structure inside them. So the first array to be the one which has the first value highest. I know there are a lot of sorting functions but i can't get the right one for this. Thanks for your help!!! Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390751 Share on other sites More sharing options...
Barand Posted November 7, 2012 Share Posted November 7, 2012 By default, a 2 dim array should sort in order of the first array elements so arsort($rArray) should do it. However, with such a ridiculous key structure you will have difficulty in comparing any of the other elements if you want to sort on another item. Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390764 Share on other sites More sharing options...
gdeliana Posted November 7, 2012 Author Share Posted November 7, 2012 (edited) I think it's not so difficult if the arrays keep their structure. I just need to sort their order by the first number you see in them. So the array that has the first value higher should be in the first place. If i use your code $rArray is my entire multidimensional array, but i should specify it that i need the SUB arrays sorted by their first value. I used your code but they are not sorted from the highest to the smallest arsort($rArray, SORT_DESC); and if i do it like this: arsort($rArray[$num][$profit], SORT_DESC); i get this error: Warning: arsort() expects parameter 1 to be array, double given in C:\wamp\www\calc\calc1 - Copy.php on line 222 Edited November 7, 2012 by gdeliana Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390771 Share on other sites More sharing options...
Barand Posted November 7, 2012 Share Posted November 7, 2012 I wonder why you are finding it so difficult Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390777 Share on other sites More sharing options...
gdeliana Posted November 7, 2012 Author Share Posted November 7, 2012 Well, i'm also wondering the same . It shouldn't be so difficult. I found also this in stackoverflow:http://stackoverflow.com/questions/2699086/sort-multidimensional-array-by-value-2, I think that we should specify the criteria of sorting. Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390779 Share on other sites More sharing options...
Barand Posted November 7, 2012 Share Posted November 7, 2012 OK Here's an example <?php $a = array ( array( 'a' => 3, 'b' => 2 ), array( 'a' => 8, 'b' => 5 ), array( 'a' => 5, 'b' => 3 ) ); echo '<pre>Unsorted '.print_r($a, 1).'</pre>'; arsort($a); // sort the sub arrays echo '<pre>Sorted '.print_r($a, 1).'</pre>'; ?> And here is the output Unsorted Array ( [0] => Array ( [a] => 3 [b] => 2 ) [1] => Array ( [a] => 8 [b] => 5 ) [2] => Array ( [a] => 5 [b] => 3 ) ) Sorted Array ( [1] => Array ( [a] => 8 [b] => 5 ) [2] => Array ( [a] => 5 [b] => 3 ) [0] => Array ( [a] => 3 [b] => 2 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390782 Share on other sites More sharing options...
gdeliana Posted November 7, 2012 Author Share Posted November 7, 2012 (edited) This is the result: arsort($rArray, SORT_DESC); var_dump($rArray); [b]array[/b] [i](size=13)[/i] 10 => [b]array[/b] [i](size=6)[/i] 95746 => float 95746.118300622 6279 => float 6279.5408451346 1002932 => float 1002932.0669677 18677 => float 18677.945713529 12682 => float 12682.417945625 13854 => float 13854.450434711 9 => [b]array[/b] [i](size=6)[/i] 95746 => float 95746.118300622 6279 => float 6279.5408451346 1002932 => float 1002932.0669677 18677 => float 18677.945713529 12682 => float 12682.417945625 13854 => float 13854.450434711 11 => [b]array[/b] [i](size=6)[/i] 95746 => float 95746.118300622 6279 => float 6279.5408451346 1002932 => float 1002932.0669677 18677 => float 18677.945713529 12682 => float 12682.417945625 13854 => float 13854.450434711 12 => [b]array[/b] [i](size=6)[/i] 47967 => float 47967.660887516 6611 => float 6611.3356882811 1002419 => float 1002419.5001146 9210 => float 9210.0558512346 11268 => float 11268.25030132 12896 => float 12896.497848447 13 => [b]array[/b] [i](size=6)[/i] 48040 => float 48040.653648759 6610 => float 6610.8287941058 1002342 => float 1002342.6441463 9209 => float 9209.3497119785 11268 => float 11268.25030132 12896 => float 12896.497848447 8 => [b]array[/b] [i](size=6)[/i] 98481 => float 98481.424684471 6260 => float 6260.5456619134 999898 => float 999898.26579654 18621 => float 18621.446200304 9511 => float 9511.8134592191 13854 => float 13854.450434711 7 => [b]array[/b] [i](size=6)[/i] 95548 => float 95548.769055707 6280 => float 6280.911326002 1003150 => float 1003150.9522721 18682 => float 18682.022089155 12682 => float 12682.417945625 13854 => float 13854.450434711 3 => [b]array[/b] [i](size=6)[/i] 105748 => float 105748.35846424 6210 => float 6210.0808439984 1002257 => float 1002257.2803817 20586 => float 20586.115871946 12984 => float 12984.067051625 14053 => float 14053.750153676 2 => [b]array[/b] [i](size=6)[/i] 105748 => float 105748.35846424 6210 => float 6210.0808439984 1002257 => float 1002257.2803817 20586 => float 20586.115871946 12984 => float 12984.067051625 14053 => float 14053.750153676 4 => [b]array[/b] [i](size=6)[/i] 105748 => float 105748.35846424 6210 => float 6210.0808439984 1002257 => float 1002257.2803817 20586 => float 20586.115871946 12984 => float 12984.067051625 14053 => float 14053.750153676 5 => [b]array[/b] [i](size=6)[/i] 95622 => float 95622.77502255 6280 => float 6280.3973956767 1003068 => float 1003068.8702829 18680 => float 18680.493448295 12682 => float 12682.417945625 13854 => float 13854.450434711 6 => [b]array[/b] [i](size=6)[/i] 95622 => float 95622.77502255 6280 => float 6280.3973956767 1003068 => float 1003068.8702829 18680 => float 18680.493448295 12682 => float 12682.417945625 13854 => float 13854.450434711 1 => [b]array[/b] [i](size=6)[/i] 105748 => float 105748.35846424 6210 => float 6210.0808439984 1002257 => float 1002257.2803817 20586 => float 20586.115871946 12984 => float 12984.067051625 14053 => float 14053.750153676 I don't understand by which criteria they got sorted, but they are not sorted by their first value. I also used the rsort() but it simply reindexes them and the results stay the same. Edited November 7, 2012 by gdeliana Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390786 Share on other sites More sharing options...
gdeliana Posted November 7, 2012 Author Share Posted November 7, 2012 (edited) It's working actually , THANKS A LOT!!!!!!! I just had the indexes of the sub arrays assigned as values also, which i had to rename only . Now i will echo the results, let's see Edited November 7, 2012 by gdeliana Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390788 Share on other sites More sharing options...
Barand Posted November 7, 2012 Share Posted November 7, 2012 Not one to learn by example, are you? If you must use a sort flag, use a valid one (though the default should work) eg arsort($a, SORT_NUMERIC); I also showed you how to get "pretty" results from print_r() Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390790 Share on other sites More sharing options...
gdeliana Posted November 7, 2012 Author Share Posted November 7, 2012 Thanks a lot!!! I am just a beginner, i have 3 weeks that have started programming at all . The last thing that i wanted to ask is: Can i use these results as php variables again? Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390795 Share on other sites More sharing options...
Muddy_Funster Posted November 7, 2012 Share Posted November 7, 2012 erm....they never stoped being variables.... Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390796 Share on other sites More sharing options...
gdeliana Posted November 7, 2012 Author Share Posted November 7, 2012 Ok, let's say i want from the sorted multidimensional array to output the third value of each array. I did like this as suggested by Muddy_Funster for($counter = 0; $counter <= count($rArray)-1; $counter++){ foreach($rArray[$counter] as $key => $value){ echo "$key = $value <br />"; } echo "<hr/>"; } just a lil bit modified but works the same. It outputs every value. My problem now is since i got the results sorted by the first value, i cannot change it now (unless i sort by second value, but i don't know how to do that) and in the output i want the third value to be outputted first. Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390800 Share on other sites More sharing options...
Barand Posted November 7, 2012 Share Posted November 7, 2012 Stop using values as keys and do it as I suggested earlier. http://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/?do=findComment&comment=1390503 Once you have a consistent key structure then sorting on the other items becomes feasible. Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390804 Share on other sites More sharing options...
gdeliana Posted November 7, 2012 Author Share Posted November 7, 2012 Yess , i did it . You are legends for me guys!!! Especially Berand!!!! Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390809 Share on other sites More sharing options...
Barand Posted November 7, 2012 Share Posted November 7, 2012 Do you want to mark this topic as solved? Button at top of page. Quote Link to comment https://forums.phpfreaks.com/topic/270349-putting-php-while-loop-variables-in-an-array-and-than-output-them-sorted-by-one-of-the-variables/#findComment-1390836 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.