2011-04-26 3 views
1

배열의 여러 결과를 하나의 변수로 결합하려고합니다.배열의 데이터를 하나의 변수로 결합

$sqlNameForCode = "Select dim_InvoiceRef from dimensions"." Where dim_FileRef = '".$addRow[$FieldName]."'"; 
$qryNameForCode = mysql_Query($sqlNameForCode); 
While($arrNameForCode = mysql_fetch_array($qryNameForCode)) { 
    $addRow[$FieldName] = $arrNameForCode['dim_InvoiceRef']; 
} 

나는 배열에서 가져온 모든 필드를 포함하는 변수 $addRow[$FieldName]이 필요합니다. 그러나 While 루프 내에 있기 때문에 마지막 필드 만 변수에 남습니다.

예, 쿼리가 내가 그냥 Orange 동일 순간에, Apple Banana Orange을 보여 echo $addRow[$FieldName]을 필요로하는 다음과 같은 결과

Apple 
Banana 
Orange 

를 가져옵니다.

도움을 주시면 감사하겠습니다.

답변

0
다음

찾을 수있는 MySQL을 기반 솔루션의에 할당 배열로 그것을해야 쉽게 :

$sql = "SELECT GROUP_CONCAT(dim_InvoiceRef SEPARATOR ' ') FROM dimensions WHERE dim_FileRef = '".$field."'"; 

$query = mysql_query($sql); 
$result = mysql_fetch_row($query); 

echo $result[0]; // Should show 'Apple Orange Banana' etc. 

나는 이해하기 쉽게하기 위해 예제에서 몇 가지 변수의 이름을 자유롭게 사용했습니다. 나중에 프로그램에서 동일한 데이터로 다른 작업을 수행해야하는 경우가 아니라면이 작업이 가능합니다.

1

당신은

While($arrNameForCode = mysql_fetch_array($qryNameForCode)) { 
    $addRow[$FieldName][] = $arrNameForCode['dim_InvoiceRef']; //notice the extra braces 
} 

echo implode(' ', $addRow[$FieldName]); //prints the values in the array separated by a space 

또는 직접 문자열

$addRow[$FieldName] = "";//defaults 
While($arrNameForCode = mysql_fetch_array($qryNameForCode)) { 
    $addRow[$FieldName] .= $arrNameForCode['dim_InvoiceRef']; //string concatenation 
} 
echo $addRow[$FieldName]; 
관련 문제