2009-08-17 10 views

risposta

12

Si potrebbe fare suddividendo la matrice utilizzando array_keys e array_values, poi entrambi giunzione, quindi unire di nuovo.

$insertKey = 'k1'; 

$keys = array_keys($arr); 
$vals = array_values($arr); 

$insertAfter = array_search($insertKey, $keys) + 1; 

$keys2 = array_splice($keys, $insertAfter); 
$vals2 = array_splice($vals, $insertAfter); 

$keys[] = "myNewKey"; 
$vals[] = "myNewValue"; 

$newArray = array_merge(array_combine($keys, $vals), array_combine($keys2, $vals2)); 
+0

C'è un errore una tantum in questa soluzione. Può essere corretto aggiungendo $ insertAfter ++; prima delle chiamate a matrice_splice(). – keithm

+0

@keithm aggiornato. – nickf

1

Non è possibile utilizzare puntatore interno per inserire elementi.

C'è array_splice che può inserire/rimuovere/sostituire elementi e sottoarray, ma è inteso per gli array con indice intero.

Ho paura di dover ricostruire l'array per inserire l'elemento (tranne i casi in cui si desidera inserire il primo/ultimo elemento) o utilizzare un array separato con indice intero per tenere le chiavi nell'ordine desiderato.

0

Questo modo è valido per i nuovi valori senza chiavi. Non è possibile inserire un valore con una chiave e gli indici numerici verranno "ripristinati" da 0 a N-1.

$keys = array_keys($a); 
$index = array_flip($keys); 

$key = key($a); //current element 
//or 
$key = 'k1'; 

array_splice($a, $index[$key] + 1, 0, array('value')); 
1

In generale, la lista doppiamente collegata sarebbe l'ideale per questo compito.

Esiste un'implementazione incorporata di questo dal PHP 5.3, chiamato SplDoublyLinkedList e dal PHP 5.5 ha anche add method, che consente di aggiungere/inserire valori nel mezzo.

+0

In realtà, [SplDoublyLinkedList] (http://ee.php.net/manual/en/class.spldoublylinkedlist.php) permette per l'inserimento sull'indice desiderato con [SplDoublyLinkedList :: add method] (http://php.net/manual/en/spldoublylinkedlist.add.php) poiché questa [richiesta pull] (https://github.com/php/php-src)/tirare/288). Non so quando questo è stato aggiunto poiché la documentazione non menziona in quale versione di php questo è stato cambiato. – Egregore

+0

In realtà menziona la versione: (PHP 5> = 5.5.0) - Ho aggiornato la risposta per riflettere questo. –

2

ho trovato una grande risposta here che funziona davvero bene. Voglio documentarlo, in modo che altri su così possono trovare facilmente:

/* 
* Inserts a new key/value before the key in the array. 
* 
* @param $key 
* The key to insert before. 
* @param $array 
* An array to insert in to. 
* @param $new_key 
* The key to insert. 
* @param $new_value 
* An value to insert. 
* 
* @return 
* The new array if the key exists, FALSE otherwise. 
* 
* @see array_insert_after() 
*/ 
function array_insert_before($key, array &$array, $new_key, $new_value) { 
    if (array_key_exists($key, $array)) { 
    $new = array(); 
    foreach ($array as $k => $value) { 
     if ($k === $key) { 
     $new[$new_key] = $new_value; 
     } 
     $new[$k] = $value; 
    } 
    return $new; 
    } 
    return FALSE; 
} 

/* 
* Inserts a new key/value after the key in the array. 
* 
* @param $key 
* The key to insert after. 
* @param $array 
* An array to insert in to. 
* @param $new_key 
* The key to insert. 
* @param $new_value 
* An value to insert. 
* 
* @return 
* The new array if the key exists, FALSE otherwise. 
* 
* @see array_insert_before() 
*/ 
function array_insert_after($key, array &$array, $new_key, $new_value) { 
    if (array_key_exists ($key, $array)) { 
    $new = array(); 
    foreach ($array as $k => $value) { 
     $new[$k] = $value; 
     if ($k === $key) { 
     $new[$new_key] = $new_value; 
     } 
    } 
    return $new; 
    } 
    return FALSE; 
} 
Problemi correlati