2013-05-01 111 views
1

所以我想在我的數據庫中的圖像的鏈接:動態PHP數組結構

$findMyImages = "SELECT link FROM images WHERE model_id ='{$me['id']}'"; 
$imageResult = mysql_query($findMyImages) or die (mysql_error()); 

$result_array = array(); 
while($row = mysql_fetch_array($imageResult)) 
{ 
    $result_array[] = $row; 
} 

print_r($result_array); 

print_r();返回此:

Array ( 
    [0] => Array (
     [0] => http://scoutsamerica.com/uploads/529746_10200706796941357_1747291081_n.jpg 
     [link] => http://scoutsamerica.com/uploads/529746_10200706796941357_1747291081_n.jpg 
    ) 
    [1] => Array (
     [0] => http://scoutsamerica.com/uploads/64311_10200924054292655_1770658989_n.jpg 
     [link] => http://scoutsamerica.com/uploads/64311_10200924054292655_1770658989_n.jpg 
    ) 
) 

我正在尋找類似的東西:

Array ( 
    [0] => http://scoutsamerica.com/uploads/529746_10200706796941357_1747291081_n.jpg 
    [1] => http://scoutsamerica.com/uploads/64311_102n_image.jpg 
    [2] => http://scoutsamerica.com/uploads/face.jpg 
    [3] => http://scoutsamerica.com/uploads/another_image.jpg 
) 

我該怎麼做?

+0

我知道我不是在尋找mysql_fetch_assoc();我想要鑰匙(它們被稱爲鑰匙,不是?)是一個整數索引。 – 2013-05-01 23:52:51

+1

* [PDO](http://php.net/manual/en/book.pdo.php)rant here * – moonwave99 2013-05-01 23:54:27

+0

是的,我同意@ moonwave99,檢查PDO,它更安全並且是最新的。針對mysql_的開發已被佔用,因此其過時且容易受到攻擊。 – Xethron 2013-05-02 00:05:02

回答

3

它因爲你正在將結果數組添加到一個新的數組。只需從結果數組中選擇想要的信息並將其放入一個新數組中即可。

例如:

while($row = mysql_fetch_array($imageResult)) 
{ 
    $result_array[] = $row[0]; 
} 

OR:

while($row = mysql_fetch_array($imageResult)) 
{ 
    $result_array[] = $row['link']; 
} 
+0

第一個人工作。謝啦! – 2013-05-01 23:59:00

+1

太棒了!兩者都可以工作:) 但是將來,0會從數據庫中選擇第一個返回的值,而'link'將從名爲'link'的列返回值。因此,如果您計劃稍後進行數據庫更改,那麼'鏈接'可能會稍微更加準確。 – Xethron 2013-05-02 00:01:36

1

通過元素附加元素:

$result_array[] = $row[0]; 
// $result_array[] = $row[1]; this is the one you want to get rid of 
1

指定你只想數字,而不是兩個:

$row = mysql_fetch_array($imageResult, MYSQL_NUM)[0]; 

,或者如果你是一個較老版本的PHP:

$row = mysql_fetch_array($imageResult, MYSQL_NUM); 
$row = $row[0]; 

默認值是:

array mysql_fetch_array (resource $result [, int $result_type = MYSQL_BOTH ]) 

你可以在括號中看到它說的兩個,它告訴它給你關聯和數字。如果你不想要,你必須指定你想要的那一個。

+0

不,還是給了我一個2個數組的數組,現在只有每個數組中的元素。 – 2013-05-01 23:57:16