1
     $array = array(
        '2' => 'a',
        '5' => 'b',
        '1' => 'c',
        '5' => 'd',
        '3' => 'e'
    )

foreach($array as $key => $value){

        $array1 = $array;
        foreach($array1 as $key1 => $value1){
            if($key > $key1){
                $result[$key1] = $value1;
            }
        }
}

    print_r($result);

this is my output:

Array
(
    [1] => c
    [2] => a
    [3] => e
)

i am making comparison of key with same key by storing this array in another array, if num > num in this case the maximum number 5(5>5) this condtions fails so 5 is not in the new array. so please can anyone tell me how this will sort or is there any better way. thanks in advance.

5
  • 1
    And the reason we would be reinventing the wheel is...? Commented Jul 18, 2014 at 4:33
  • Most likely for homework if I had to guess. They probably want you to use sorting methods such as the grunt, insertion, selection, etc. Take a look at the pseudo-code, they provide a better outline than any answer. Commented Jul 18, 2014 at 4:40
  • 2
    in array the key '5' is repeating two times.An array can't have same key multiple times Commented Jul 18, 2014 at 4:59
  • this is my test questions and there are restrictions on it to not to use any php built in functions of php. i googled a lot but nothing found, at last i am posting this question. Commented Jul 18, 2014 at 5:06
  • yes the key is repeating @rack_nilesh. if we print_r this array only latest one will be printed. but one key '5' will remained in array and duplicate will be removed. so this is not the headache. Commented Jul 18, 2014 at 5:09

1 Answer 1

2

Your algorithm does not work because the outer loop iterate over each key, and inner loop will try to insert any keys less than the current key into the array. Although these keys are smaller than the current key, but it does not guarantee they are in ascending order. For example, the following array will not work:

array(
  3 => 'a',
  2 => 'b',
  1 => 'c'
);

Of course it has the issue of missing some element from the original array as you may have already noticed.

Instead you can use any sorting algorithm (like mergesort, quicksort, etc) to sort the keys first, then build the new associative array. Below implements insertion_sort (because it is easy to do).

function insertion_sort($arr)
{
    for ($i = 0; $i < count($arr); $i++) {
        $j = $i;
        while ($j > 0 && $arr[$j] < $arr[$j-1]) {
            $tmp = $arr[$j-1];
            $arr[$j-1] = $arr[$j];
            $arr[$j] = $tmp;
            $j--;
        }
    }

    return $arr;
}

$array = array(
    '2' => 'a',
    '5' => 'b',
    '1' => 'c',
    '5' => 'd',
    '3' => 'e'
);
$keys = array_keys($array);

$sorted = array();
foreach (insertion_sort($keys) as $key) {
    $sorted[$key] = $array[$key];
}

print_r($sorted);

prints

Array
(
    [1] => c
    [2] => a
    [3] => e
    [5] => d
)
Sign up to request clarification or add additional context in comments.

Comments

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.