2016-09-17 5 views
2

안녕하세요. 내 MySQL DB에서 일부 레코드가 있습니다. 나는 그래서 같은 다차원 연관 배열을 채울 가지고 :PHP에서 다차원 배열 채우기

results{ 
    "key#1": array 
      array 
      array 
    "key#2": array 
      ... 
} 

내 데이터 구조를 만들려면 다음 코드를 사용하지만 내 연관 다차원 배열에 데이터를 푸시 할 지 모르겠어요.

while ($row = mysql_fetch_array($result, MYSQL_NUM)) { 
    $nome_paziente = $row[0]; 
    $numero_richiesta = $row[1]; 
    $importo_manuale = $row[2]; 
    $sconto_totale = $row[3] + $row[4]; 
    $importo_finale = round(($row[5] - (($row[5] * $sconto_totale)/100)),2); 
    $id_centro_operativo = $row[7]; 
    $descrizione = $row[9]; 
    $codice_convenzione = $row[10]; 

    $elements = array(
     'nome_paziente' => $nome_paziente, 
     'numero_richiesta' => $numero_richiesta, 
     'importo_manuale' => $importo_manuale, 
     'importo_finale' => $importo_finale, 
     'descrizione' => $descrizione, 
     'codice_convenzione' => $codice_convenzione 
    ); 

    $key = $row[8]."#sep#".$row[6]; //nome_studio#data_appuntamento 
    $results[$key] = $elements; 
} 

var_dump($results); 

각 키에는 하나의 배열 만 있습니다.

답변

2

당신은 $key 다른 나는 완전히이 누락 된, $results[$key]

$key = $row[8]."#sep#".$row[6]; //nome_studio#data_appuntamento 
if (array_key_exists($key,$results)) { 
$results[$key][] = $elements; 
} 
else{ 
$results[$key] = array(); 
$results[$key][] = $elements; 
} 
+0

이 감사에 $elements를 추가 $result에 존재하지 않는 경우마다 사용이 배열로 $results[$key]을 값 $results[$key]을 무시하고 있습니다. :-) –

+0

당신은 환영합니다 – Nitin9791

관련 문제