2015-04-22 11 views
8

COMPANY ARRAYPHP Array che unisce non funziona

array(1) { 
    [0]=> array(19) { 
    ["entityid"]=> string(4) "3626" 
    ["entityparentid"]=> string(1) "0" 
    ["entityduplicateof"]=> string(1) "0" 
    ["entitytype"]=> string(1) "0" 
    ["entityname"]=> string(12) "Facebook Inc" 
    } 
} 

DISTANZA ARRAY

array(1) { 
    ["distance"]=> string(4) "1.22" 
} 

Quello che mi piacerebbe l'uscita a guardare come:

array(1) { 
    [0]=> array(19) { 
     ["entityid"]=> string(4) "3626" 
     ["entityparentid"]=> string(1) "0" 
     ["entityduplicateof"]=> string(1) "0" 
     ["entitytype"]=> string(1) "0" 
     ["entityname"]=> string(12) "Facebook Inc" 
     ["distance"]=> string(4) "1.22" // here 
    } 
} 

Domanda:

array_push($company_array,$distance_array); sembra non fare quello che voglio che faccia.

Si aggiunge alla fine, ma non dove voglio che (notare la differenza di cui è inserito):

array(1) { 
    [0]=> array(19) { 
     ["entityid"]=> string(4) "3626" 
     ["entityparentid"]=> string(1) "0" 
     ["entityduplicateof"]=> string(1) "0" 
     ["entitytype"]=> string(1) "0" 
     ["entityname"]=> string(12) "Facebook Inc" 
    }, 

    ["distance"]=> string(4) "1.22" // not here 
} 

risposta

4

Ha un altro livello all'interno $company, se si desidera che il singolo array interno che un altro nidificazione, puntare a indice zero direttamente, e utilizzare array_merge:

$company[0] = array_merge($company[0], $distance); 

Sample Output

1

un altro modo per unire i due array rappresenta l'+ esercente:

$company[0] = $company[0] + $distance; 

Una spiegazione dettagliata della differenza tra array_merge e + può essere trovato here.