2010-07-12 7 views
0

루프를 통해 아래의 배열을 만들 수 있습니까? 텍스트는 데이터베이스에서 생성됩니다!루프를 통해 다차원 배열 만들기!

$listofimages = array(
     array( 'titre' => 'PHP', 
       'texte' => 'PHP (sigle de PHP: Hypertext Preprocessor), est un langage de scripts (...)', 
       'image' => './images/php.gif' 
     ), 
     array( 'titre' => 'MySQL', 
       'texte' => 'MySQL est un système de gestion de base de données (SGDB). Selon le (...)', 
       'image' => './images/mysql.gif' 
     ), 
     array( 'titre' => 'Apache', 
       'texte' => 'Apache HTTP Server, souvent appelé Apache, est un logiciel de serveur (...)', 
       'image' => './images/apache.gif' 
     ) 
    ); 
+0

이 심지어 것 같다 OP의 질문 – Thariama

답변

0

뭔가 :

$listofimages = array(); 

foreach ($rowset as $row) { 

    $listofimages[] = array(
     'titre' => $row['titre'], 
     'texte' => $row['texte'], 
     'image' => $row['image'] 
    ); 

} 

?

-1
foreach ($listofimages as $image) { 
    echo '<img src="'.$image['image'].'" />'; 
} 
+0

에서 인출 한 정보를 제공하십시오 나는 "로 질문을 잘못 해석 루프와 같은 배열을 만들려면 어떻게해야합니까? " – Starx

+0

에 응답하지 않는 데이터베이스 – Sjoerd

1

같은 것을 할,

$result = mysql_query("SELECT * FROM table;"); 
while($row = mysql_fetch_assoc($result) { 
    //$row will hold all the fields's value in an array 
    $mymainarray[] = $row; //hold your array into another array 
} 

//To display 
echo "<pre>"; 
print_r($mymainarray); 
echo "</pre>"; 
0

만 다음 단지 (MySQL은 가정)되는 필드 titre, 쿼리에 texteimage 선택하면 :

$listofimages = array(); 

$result = mysql_query("SELECT titre, texte, image FROM table"); 

while((row = mysql_fetch_assoc($result))) { 
    $listofimages[] = $row; 
} 

mysql_fetch_assoc()은 연관성에서 결과를 가져옵니다. 배열. 같은

+0

내 질문도 다른 사람들과 매우 유사하기 때문에 downvoted입니까? 그건 말도 안돼 ... –

0

의이 쿼리를 수행하고 그 결과 식별자를 얻을 수 있다고 가정 해 봅시다 :

$result = mysql_query("SELECT * FROM table;"); 

지금 당신이 mysql_fetch_assoc($result)를 사용하여 배열로 모든 행을 얻을 수 있습니다. 지금은 단지 최종 배열을 만들고 거기에 모든 배열을 추가

$final_array = array(); 
while($row = mysql_fetch_assoc($result) !== false) { 
    $final_array[] = $row; 
} 

print_r($final_array); 
+0

이것은 mysql_fetch_assoc ($ result)의 사용을 제외하고는 내 대답의 동일한 복사본입니다; – Starx

+0

@Starx : 답변이 잘못되었습니다 (또는 적어도 100 % 정확하지는 않음)). mysql_fetch_array를 사용하면 연관 ** 및 ** 숫자 배열로 행을 가져옵니다. 따라서 모든 열은 배열에 두 번 포함됩니다. –

+0

+1은 하향 보를 보상합니다. –

0
$mult_array = array(); 

$result = mysql_query("SELECT col1,col2,... FROM table"); 

if(mysql_num_rows($result)>0){ 
    while((row = mysql_fetch_assoc($result)) { 
     $mult_array[] = $row; 
    } 
} 

// if u want to pick some specific cols from data selected from database then- 



if(mysql_num_rows($result)>0){ 
$temp_arr = array(); 
    while((row = mysql_fetch_assoc($result)) { 
     $temp_arr['col1'] = $row['col1']; 
     $temp_arr['col2'] = $row['col2']; 
     //... so on 
     $mult_array[] = $temp_arr; 
    } 
}