2011-09-22 12 views
6

Ho una stringa del tipo:tasti array dinamico

$string = 'one/two/three/four';

quale trasformarlo in un array:

$keys = explode('/', $string);

Questa matrice può avere qualsiasi numero di elementi, come 1 , 2, 5, ecc.

Come posso assegnare un determinato valore a un array multidimensionale, ma utilizzare lo $keys creato ove identificare la posizione in cui inserire?

come:

$arr['one']['two']['three']['four'] = 'value';

Scusate se la domanda è confusa, ma non so come spiegarlo meglio

risposta

13

Questa è una specie di non banale, perché si vuole nido, ma dovrebbe andare qualcosa del tipo:

function insert_using_keys($arr, $keys, $value){ 
    // we're modifying a copy of $arr, but here 
    // we obtain a reference to it. we move the 
    // reference in order to set the values. 
    $a = &$arr; 

    while(count($keys) > 0){ 
     // get next first key 
     $k = array_shift($keys); 

     // if $a isn't an array already, make it one 
     if(!is_array($a)){ 
      $a = array(); 
     } 

     // move the reference deeper 
     $a = &$a[$k]; 
    } 
    $a = $value; 

    // return a copy of $arr with the value set 
    return $arr; 
} 
1

Prima di tutto è necessario accertarsi che la chiave esista, quindi assegnare il valore. Qualcosa del genere dovrebbe funzionare (non testata):

function addValueByNestedKey(&$array, $keys, $value) { 
    $branch = &$array; 
    $key = array_shift($keys); 
    // add keys, maintaining reference to latest branch: 
    while(count($keys)) { 
     $key = array_pop($keys); 
     if(!array_key_exists($key, $branch) { 
      $branch[$key] = array(); 
     } 
     $branch = &$branch[$key]; 
    } 
    $branch[$key] = $value; 
} 

// usage: 
$arr = array(); 
$keys = explode('/', 'one/two/three/four'); 

addValueByNestedKey($arr, $keys, 'value'); 
1

E 'banale, ma:

function setValueByArrayKeys($array_keys, &$multi, $value) { 
    $m = &$multi 
    foreach ($array_keys as $k){ 
     $m = &$m[$k]; 
    } 
    $m = $value; 
} 
+0

Questo non funzionerà. '$ multi = $ multi [$ k]' crea una * copia *, non è un riferimento, quindi non stai effettivamente modificando nulla quando fai '$ multi = $ valore', ad eccezione dell'array finale. –

+0

lo hai appena modificato. – MattoTodd

+0

esso * ancora * non funzionerà, stai creando * copie *, non * riferimenti *. –

5
$string = 'one/two/three/four'; 
$keys = explode('/', $string); 
$arr = array(); // some big array with lots of dimensions 
$ref = &$arr; 

while ($key = array_shift($keys)) { 
    $ref = &$ref[$key]; 
} 

$ref = 'value'; 

Quello che sta facendo:

  • Utilizzando una variabile, $ref, per tenere traccia di un riferimento alla dimensione corrente di $arr.
  • Looping tramite $keys uno alla volta, con riferimento all'elemento $key del riferimento corrente.
  • Impostazione del valore sul riferimento finale.
+0

Ottengo una "connessione chiusa dal server" quando provo questo :) – Alex

+0

ok ora funziona: D – Alex

+0

Cosa succede se '$ keys' contiene 0 o NULL? –

Problemi correlati