PHP - 关联数组:更改键值对的键,其中value是类对象

I have an associative array of the form:

$input = array("one" => <class object1>,
               "two" => <class object2,
               ... //and so on);

The keys of $input are guaranteed to be unique. I also have a method called moveToHead($key) which moves the $input[$key] element to the 0th location of this associative array. I have few questions:

  1. Is it possible to determine the index of an associative array?
  2. How to move the array entry for corresponding $key => $value pair to the index 0 and retaining the $key as is?
  3. What could be the best possible way to achieve both of the above points?

I was thinking to do array_flip for 2nd point (a sub solution), but later found out that array_flip can only be done when array elements are int and string only. Any pointers?

With a function called array_keys you can determine the index of a key:

$keys = array_flip(array_keys($input));
printf("Index of '%s' is: %d
", $key, $keys[$key]);

To insert an array at a certain position (for example at the beginning), there is the array_splice function. So you can create the array to insert, remove the value from the old place and splice it in:

$key = 'two';
$value = $input[$key];
unset($input[$key]);    
array_splice($input, 0, 0, array($key => $value));

Something similar is possible with the array union operator, but only because you want to move to the top:

$key = 'two';
$value = $input[$key];
unset($input[$key]);
$result = array($key => $value) + $input;

But I think this might have more overhead than array_splice.

The "index" of an associative array is the key. In a numerically indexed array, the "key" is the index number.

EDIT: I take it back. PHP's associative arrays are ordered (like Ordered Maps in other languages).

But perhaps what you really want is an ordered array of associative arrays?

$input = array(array("one" => <class object1>),
               array("two" => <class object2>)
               //...
         );

You can use internal array pointer functions to do what you want:

$input = array(
    "one"   => "element one",
    "two"   => "element two",
    "three" => "element three",
);
reset($input); // Move to first element.
echo key($input); // "one"

You can unset the element out and put it in the front:

$input = array(
    "one"   => "element one",
    "two"   => "element two",
    "three" => "element three",
);
$key = "two";
$element = $input[$key];
unset($input[$key]);
// Complicated associative array unshift:
$input = array_reverse($input);
$input[$key] = $element;
$input = array_reverse($input);
print_r($input);

Here's what I came up with:

$arr = array('one' => 'Value1', 'two' => 'Value2', 'three' => 'Value3');

function moveToHead($array,$key) {
   $return = $array;
   $add_val = array($key => $return[$key]);
   unset($return[$key]);
   return $add_val + $return;
}

print_r(moveToHead($arr,'two'));

results in

Array
(
    [two] => Value2
    [one] => Value1
    [three] => Value3
)

http://codepad.org/Jcb6ebxZ