karl-93 Posted January 14, 2013 Share Posted January 14, 2013 I ordered an array: code: for ($ x = 0; $ x <count ($ arr); $ x + +) { for ($ y = 0; $ y <count ($ arr); $ y + +) { if ($ arr [$ x] <$ arr [$ y]) { $ temp = $ arr [$ x]; $ arr [$ x] = $ arr [$ y]; $ arr [$ y] = $ temp; } } } The code works ... now I have to print the array, formatted as follows: [5] => -9 I have to keep the original key and returning the key and value according to the following format described above. How should I do? Thank you! Ps. I can not use sorting functions. Quote Link to comment Share on other sites More sharing options...
cpd Posted January 14, 2013 Share Posted January 14, 2013 If the code above is your actual code you could of used the sort() function; I appreciate you said you can't but never the less. To print the code use: foreach($arr as $k => $v) echo $k . " => " . $v . "<br />"; This sentence I have to keep the original key and returning the key and value according to the following format described above. makes absolutely no sense to me. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 14, 2013 Share Posted January 14, 2013 Ps. I can not use sorting functions. Why not? Show us this array and how you want it sorted echo '<pre>',print_r($array, true),'</pre>'; Quote Link to comment Share on other sites More sharing options...
karl-93 Posted January 14, 2013 Author Share Posted January 14, 2013 I can not use sorting functions because the tutorial says not to. Here is the array: $arr=array(55,32,3,89,88,-13,155,485,-77,40,91,13,77,44,-511,2); This sentence: I have to keep the original key and returning the key and value according to the following format described above. It means that I have to print this way: 14 => -511 8 => -77 5 => -13 15 => 2 ... 7 => 485 I do not print well: 0 => -511 1 => -77 2 => -13 3 => 2 ... 15 => 485 How should I do? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 14, 2013 Share Posted January 14, 2013 By tutorial you mean homework, right? Quote Link to comment Share on other sites More sharing options...
karl-93 Posted January 14, 2013 Author Share Posted January 14, 2013 Yes! I'm no expert in the programming language PHP. I'm following this tutorial to learn how to program and tutorial there are tasks to be performed. how should I do to solve my problem(homework)? Thanks! Quote Link to comment Share on other sites More sharing options...
karl-93 Posted January 14, 2013 Author Share Posted January 14, 2013 Help me! :'( Thanks! Quote Link to comment Share on other sites More sharing options...
cpd Posted January 14, 2013 Share Posted January 14, 2013 (edited) You would need to store the key value pair somewhere else. I would then use the usort() function to make a comparison and have PHP sort it. For example, if you rework the array to form another array in the format array(0 => "key:value", 1 => "key:value") you can use: usort($array, function($a, $B) { $a = explode(":", $a); $b = explode(":", $B); if($a[1] < $b[1]) { return -1; } elseif($a[1] == $b[1]) { return 0; } else { return 1; } }); Edited January 14, 2013 by cpd Quote Link to comment Share on other sites More sharing options...
kicken Posted January 14, 2013 Share Posted January 14, 2013 Sort an array of keys based on their values, then after that array of keys is sorted, grab the original values for each key. Untested, but rough idea: $keys = array_keys($arr); for ($i=0; $i<count($keys); $i++){ for ($n=0; $n<$count($keys); $n++){ $a = $arr[$keys[$i]]; $b = $arr[$keys[$n]]; if ($a < $B){ $tmp=$keys[$n]; $keys[$n] = $keys[$i]; $keys[$i] = $tmp; } } } $final = array(); foreach ($keys as $key){ $final[$key]=$arr[$key]; } print_r($final); Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 15, 2013 Share Posted January 15, 2013 Karl-93: If you start anew and use the foreach () loop instead of for (), I think you'll find that this task becomes a lot easier. At least, getting to the key for any given value will be. The rest you've solved already. Quote Link to comment Share on other sites More sharing options...
karl-93 Posted January 15, 2013 Author Share Posted January 15, 2013 I solved. Thanks! 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.