ilişkisel diziler için array_splice ()

7 Cevap php

Ben bir ilişkisel dizi var ki:

array(
  "color" => "red",
  "taste" => "sweet",
  "season" => "summer"
);

ve ben bunun içine yeni bir öğe tanıtmak istiyorum:

"texture" => "bumpy" 

2. öğenin arkasında ama tüm dizi anahtarları koruyarak:

array(
  "color" => "red",
  "taste" => "sweet",
  "texture" => "bumpy", 
  "season" => "summer"
);

Bunu yapmak için bir işlevi var? array_splice() kesmek değil, sadece sayısal tuşları ile çalışabilirsiniz.

7 Cevap

Ben el ile yapmak gerek:

# Insert at offset 2
$offset = 2;
$newArray = array_slice($oldArray, 0, $offset, true) +
            array('texture' => 'bumpy') +
            array_slice($oldArray, $offset, NULL, true);

Ben benzer bir yöntemle geldi ve array_splice_key olarak ayarlamak @ Luxian cevabı benzer. https://gist.github.com/4499117

/**
 * Insert another array into an associative array after the supplied key
 *
 * @param string $key
 *   The key of the array you want to pivot around
 * @param array $source_array
 *   The 'original' source array
 * @param array $insert_array
 *   The 'new' associative array to merge in by the key
 *
 * @return array $modified_array
 *   The resulting merged arrays
 */
function array_splice_after_key( $key, $source_array, $insert_array ) { 
    return array_splice_key( $key, $source_array, $insert_array );
}

/**
 * Insert another array into an associative array before the supplied key
 *
 * @param string $key
 *   The key of the array you want to pivot around
 * @param array $source_array
 *   The 'original' source array
 * @param array $insert_array
 *   The 'new' associative array to merge in by the key
 *
 * @return array $modified_array
 *   The resulting merged arrays
 */
function array_splice_before_key( $key, $source_array, $insert_array ) { 
    return array_splice_key( $key, $source_array, $insert_array, -1 );
} 

/**
 * Insert another array into an associative array around a given key
 *
 * @param string $key
 *   The key of the array you want to pivot around
 * @param array $source_array
 *   The 'original' source array
 * @param array $insert_array
 *   The 'new' associative array to merge in by the key
 * @param int $direction
 *   Where to insert the new array relative to given $position by $key
 *   Allowed values: positive or negative numbers - default is 1 (insert after $key)
 *
 * @return array $modified_array
 *   The resulting merged arrays
 */
function array_splice_key( $key, $source_array, $insert_array, $direction = 1 ){
    $position = array_search( $key, array_keys( $source_array ) ) + $direction;

    // setup the return with the source array
    $modified_array = $source_array;

    if( count($source_array) < $position && $position != 0 ) {
        // push one or more elements onto the end of array
        array_push( $modified_array, $insert_array );
    } else if ( $position < 0 ){
        // prepend one or more elements to the beginning of an array
        array_unshift( $modified_array, $insert_array );
    } else {
        $modified_array = array_slice($source_array, 0, $position, true) +
            $insert_array +
            array_slice($source_array, $position, NULL, true);
    }
    return $modified_array;
}

Ben, bir anahtar aramak normaldir gibi kesikler ve normal bir fonksiyon gibi kaldırılır eleman dönecektir tonight.It ile geldi bunu yapmak için süper basit bir yolu yoktur.

function assoc_splice($source_array, $key_name, $length, $replacement){
    return array_splice($source_array, array_search($key_name, array_keys($source_array)), $length, $replacement);
}

Soulmerge cevabı dayanarak ben bu kullanışlı fonksiyon yarattık:

function array_insert($array,$values,$offset) {
    return array_slice($array, 0, $offset, true) + $values + array_slice($array, $offset, NULL, true);  
}

Peki, sıfırdan diziyi yeniden oluşturabilirsiniz. Ama belli bir düzen içinde bir ilişkisel dizi geçmesi için en kolay yolu, ayrı bir sipariş dizi tutmaktır. Şöyle:

$order=array('color','taste','texture','season');
foreach($order as $key) {
  echo $unordered[$key];
}

Ben bunun için bir fonksiyon varsa emin değilim, ama siz, dizi yineleme indeksi saklamak ve array_push kullanabilirsiniz.