2017-01-16 9 views
2
<?php 
$content = "my content"; 
$parts = array("Part 1 :", "Part 2 :", "Part 3 :","Part 4 :", "Part 5 :"); 
$name_list = array("Part 1 :\n", "Part 2 :\n", "Part 3 :\n","Part 4 :\n", "Part 5 :"); 

$email_body = "Product:\n" .str_replace($parts, $name_list, $content); 
?> 

첫 번째 스크립트에서와 같은 결과를 얻을 수 있지만 내 데이터베이스에서 두 번째 배열 $name_list을 가져 오려고합니다.데이터베이스에서 배열로 변환

아래 스크립트를 수행했지만 작동하지 않습니다.

<?php 
$query = "SELECT * FROM my_table WHERE id LIKE '%$id%'"; 
$search = mysql_query($query) or die(mysql_error()); 

$name_list = array(); 
while ($row = mysql_fetch_array($search)) { 
    $name_list[] = $row['name'] . ":\n,"; 
} 
$name_list = explode(", ", $name_list); 
?> 

답변

2

제거 $name_list 때문에 이미 배열이 줄 $name_list = explode(",",$name_list);, 당신은, 배열에 문자열을 변환하는 폭발 사용할 수 있습니다 예를 들어.

<?php 
    $name_list = "Part 1 :\n, Part 2 :\n, Part 3 :\n, Part 4 :\n, Part 5 :"; 
    $name_list = explode(",", $name_list); 
    print_r($name_list); 
?> 

출력 그래서 코드 아래

Array 
(
    [0] => Part 1 : 

    [1] => Part 2 : 

    [2] => Part 3 : 

    [3] => Part 4 : 

    [4] => Part 5 : 
) 

시도

<?php 
$query = "SELECT * FROM my_table WHERE id LIKE '%$id%'"; 
$search = mysql_query($query) or die(mysql_error()); 


$name_list = array(); 
while ($row = mysql_fetch_array($search)) { 
    $name_list[] = $row['name'].":\n,"; 
} 

$parts = array("Part 1 :", "Part 2 :", "Part 3 :","Part 4 :", "Part 5 :"); 

$email_body = "Product:\n" .str_replace($parts, $name_list, $content); 

?> 
+0

이미 implode로 테스트했지만 작동하지 않습니다. –

+0

폭발이 필요하기 때문에이 줄 $name_list = explode(",",$name_list); 만 제거하십시오. 문자열이 필요하지만 통과 배열 – msk

+0

죄송합니다. 시도했지만 작동하지 않습니다. –