Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to make a simple script that will first put an array() into function so I can call on it multiple times to sort. Here is what my array looks like:

// I want this inside of a function so I can call on it:

$a = array(
    15 => "C",
    12 => "E",
    11 => "B",
    19 => "P",
    10 => "L",
    14 => "N",
    20 => "A"    
 );

// This is how I brought it into a function and formatted it:

function original_array($a){ 
    foreach($a as $key => $types) {
        print $key . " " . ":" . " " . $types . "<br />";
    }
}

I then just have to call original_array() and it prints out fine, but if I sort it once I cant sort it again. It will just print out the first sort:

// Print out array is is:

original_array();

// Then I print out array with sort():

sort($a);
original_array($a);

// But if I try and sort it again with different sort it doesn't work:

ksort($a);
original_array($a);

What am I doing wrong? I am somewhat new to PHP so your help is greatly appreciated.

UPDATE://

This is what I ended up doing. I should have read up on sort function a little more thoroughly. I was unaware that it destroyed the original pointers.

<?php  
// Original array: 
$a = array(
    15 => "C",
    12 => "E",
    11 => "B",
    19 => "P",
    10 => "L",
    14 => "N",
    20 => "A"   
);

// Array for sort() function:
$b = $a

function print_format($array){ 
    foreach($array as $key => $types) {
        print $key . " " . "=>" . " " . $types . "<br />";
    }
}    


print "Original";  
print_format($a);     

print "sort()";
sort($b);
print_format($b);

print "ksort()";
ksort($a);
print_format($a);    

print "asort()";
asort($a);  
print_format($a);    

print "krsort()";
krsort($a);  
print_format($a);

print "rsort()";
rsort($b);  
print_format($b);    

print "arsort()";
arsort($a);  
print_format($a);
?> 
share|improve this question

2 Answers

You can use reset() function. http://php.net/manual/en/function.reset.php

share|improve this answer
I have tried the reset. It may of been where I placed it, but it did not work. – Derek Gutierrez Mar 23 '12 at 1:56
1  
my answer is signed as unused by someone. But I offered my option only. Can you use $b=$a; and after each sort($b) call $a again with ($b=$a) therefore $a will be remained as before. – Huseyin Mar 23 '12 at 2:28
That worked perfectly! So by assigning it to a new variable as stated ($b = $a) basically creates a new array that is unmodified? – Derek Gutierrez Mar 23 '12 at 2:47
Ok. But I loosed some points because of negative pointing. Please accept mine answer. – Huseyin Mar 23 '12 at 7:51

The normal sort() function destroys the keys for all values, so when it's sorted, each key is now numeric 0, 1, 2, 3. Therefore when you use ksort(), it does nothing because they're already numerically sorted by key.

Try using asort() to maintain key => value association when sorting by value. Then when you use ksort() later on, the keys still exist so you can sort in that way.

share|improve this answer
This helped immensely. sort() function through me off. I didn't understand why the others were working but they wouldn't if sort() was before them. – Derek Gutierrez Mar 23 '12 at 2:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.