PHP - Associative arrays: changing keys for a pair of key values, where value is a class object

I have an associative array of form:

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

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

  • Is it possible to determine the index of an associative array?
  • How to transfer an array record for the corresponding pair $key => $value to index 0 and save $key as is?
  • What could be the best way to achieve both of the above points?

I was thinking of making array_flip for the second point (helper solution), but later found out that array_flip can only be executed when the elements of the array are only int and string. Any pointers?

+4
source share
4 answers

Using the array_keys function array_keys you can determine the key index:

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

To insert an array at a specific position (for example, at the beginning), there is an array_splice function. This way you can create an array to insert, remove the value from the old place and paste it into:

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

Something like this is possible using the array join operator , but only because you want to go to the beginning:

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

But I think this may have more overhead than array_splice .

+1
source

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

EDIT: I take it back. PHP arrays are corrected (for example, 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>) //... ); 
+1
source

Here is 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')); 

leads to

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

http://codepad.org/Jcb6ebxZ

+1
source

You can use internal pointer pointers 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 delete the item and put it at the beginning:

 $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); 
0
source

All Articles