Re-order arrays key in ascending order?

Hi guys,

The scenario is, the keys and values are all both in integer.
And it look like this below,


Array
(
    [] => 12
    [] => 11
    [] => 15
    [] => 16
)

As you can see I left the keys/indexes blank.
What I want to do is use the values as keys and sort them into ascending order.
And should look like this,


Array
(
    [11] => 12
    [12] => 11
    [15] => 16
    [16] => 15
)

Thanks in advance

Take a look at the PJP Manual on arrays()
http://us3.php.net/manual/en/ref.array.php

// Try playing around with this



$items = array
(
     12, 
     11,
     15,
     16,
);
echo '<pre>';
	print_r($items);
	foreach( $items as $key => $item ):	
		echo '<br />' .$key .' ==> ' .$item;
	endforeach;
  
  asort($items); 
	echo '<h2>sorted</h2>';
	print_r($items);
	
	foreach( $items as $key => $item ):	
		echo '<br />' .$key .' ==> ' .$item;
	endforeach;
echo '</pre>';


that’s not what I’m looking for, I’ll try to experiment with it.

Hello,

The code is :

<?php

$values = array(12,11,15,16);
$keys= $values;
sort($values);
$result = array_combine($values, $keys);
foreach($result as $key => $value)
echo $key .“=>”.$value.“<br>”;
?>

@dreamdezigns1 ;

Actually I already figured this out myself,


	function sortkeys() {
		$r_orig = array(
						0 => '11',
						1 => '9',
						2 => '12',
						3 => '16',
				);

		$r_copy = $r_orig; //Make duplicate array
		
		//Get smallest value first and assign to first index/key.
		if(!empty($r_orig)){
			foreach($r_orig as $k=>$v) {
				$min_val = min($r_copy);
				$del_val = min($r_copy);
				$r_orig[$min_val] = $r_orig[$k]; //Change key.
				unset($r_orig[$k]);
								
				if(($key = array_search($del_val, $r_copy)) !== false) {
				    unset($r_copy[$key]);
				}							
			}
		}
		
		echo 'Original array, Changed keys:';
		echo '<pre>';
		print_r($r_orig);
		echo '</pre>';
	}

But your code is much more beautiful because it’s concise and short.
Thanks dude, this is an eye opener for me.
I guess I need to spend more time playing with PHP array built-in functions…

Thanks again…

Problem solved.

I wish there is a “Problem solved” button.