0

can any one help me how to get the sorting of the numbers;

num1 = 1
num2 = 1
num3 = 3
num4 = 5


 $values = array($_POST["num1"] => 1, $_POST["num2"] => 2,$_POST["num3"] => 3,$_POST["num4"] =>4);
    asort($values);
    foreach($values as $key => $val){
    echo "<br>$key = $val<br>";
    }

The num1 is not printed.. and i got an out put of

1 = 2

3 = 3

4 = 4

how can i got the output complete like this?

1 = 1 | 1 = 2 | 3 = 3 | 4 = 4

2
  • 1
    you cannot assign an array key more than once! Commented Sep 29, 2011 at 10:58
  • awts T_T how can i solve that? Commented Sep 29, 2011 at 11:00

1 Answer 1

0

You have the key => value pair the wrong way round in your array, hence why your key for 1 gets overwritten.

Try replacing your $values = ... line with this:

$values = array(1 => $_POST["num1"], 2 => $_POST["num2"], 3 => $_POST["num3"], 4 => $_POST["num4"]);
2
  • what if the $val value is also inputted and there is a duplicated value,, is there a way to get the output i want? Commented Sep 29, 2011 at 11:26
  • have you tried with the newly arranged array I posted above? Nothing should get overwritten as lone as none of the numbers on the left of the => are the same... Commented Sep 29, 2011 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.