2010-07-08 22 views
63

ho cercato di spingere un elemento di un array associativo in questo modo:Push to array associativo in PHP

$new_input['name'] = array(
    'type' => 'text', 
    'label' => 'First name', 
    'show' => true, 
    'required' => true 
); 
array_push($options['inputs'], $new_input); 

Tuttavia, invece di 'nome' come chiave in aggiunge un numero. C'è un altro modo per farlo?

+0

Non è possibile inserire un array in un altro array. Ho provato tutte queste opzioni e l'unione ha appena aggiunto l'array. Ho risolto il mio problema con una classe. –

risposta

80
$options['inputs']['name'] = $new_input['name']; 
+3

aiuta alcuni come +1 –

1
$new_input = array('type' => 'text', 'label' => 'First name', 'show' => true, 'required' => true); 
$options['inputs']['name'] = $new_input; 
6

soluzione WebbieDave funzionerà. Se non si desidera sovrascrivere tutto ciò che potrebbe essere già al 'nome', si può anche fare qualcosa di simile:

$options['inputs']['name'][] = $new_input['name'];

+0

Questo non funziona se non si desidera mantenere tutto associativo, ad es. senza spingere altri array numerati in mezzo. Date un'occhiata a @Steven H sotto – brianlmerritt

3

Se $new_input possono contenere più di un elemento di 'nome' è possibile utilizzare array_merge.

$new_input = array('name'=>array(), 'details'=>array()); 
$new_input['name'] = array('type'=>'text', 'label'=>'First name'...); 
$options['inputs'] = array_merge($options['inputs'], $new_input); 
44

Invece di array_push(), utilizzare array_merge()

$existing_array = array('a'=>'b', 'b'=>'c'); 
$new_array = array('d'=>'e', 'f'=>'g'); 

array_merge($existing_array, $new_array); 

suoi rendimenti la matrice risultante accoda nella matrice iniziale in questo caso $existing_array.

E matrice risultante sarà

array('a'=>'b', 'b'=>'c','d'=>'e', 'f'=>'g') 

prega su questo link, di essere a conoscenza di eventuali problemi.

+0

in realtà dovrebbe essere $ existing_array = array ('a' => 'b', 'b' => 'c'); $ new_array = array ('d' => 'e', ​​'f' => 'g'); $ result = array_merge ($ array_esistente, $ nuovo_array); –

+1

beh, è ​​un buon senso raccogliere l'output. Stavo solo dando l'idea. Saluti –

3

La risposta di Curtis era molto vicina a ciò di cui avevo bisogno, ma l'ho modificata un po '.

dove ha usato:

$options['inputs']['name'][] = $new_input['name']; 

ho usato:

$options[]['inputs']['name'] = $new_input['name']; 

Ecco il mio codice vero e proprio utilizzando una query da un DB:

while($row=mysql_fetch_array($result)){ 
    $dtlg_array[]['dt'] = $row['dt']; 
    $dtlg_array[]['lat'] = $row['lat']; 
    $dtlg_array[]['lng'] = $row['lng']; 
} 

Grazie!

7

Questa è una funzione di fresco

function array_push_assoc($array, $key, $value){ 
$array[$key] = $value; 
return $array; 
} 

Basta usare

$myarray = array_push_assoc($myarray, 'h', 'hello'); 

Credits & Explanation

2

basta cambiare qualche frammento (utilizzare array_merge funzione): -

$options['inputs']=array_merge($options['inputs'], $new_input); 
0

Puoi provare.

$options['inputs'] = $options['inputs'] + $new_input;