1

所以我想在我的数据库中获取所有图像的链接:

$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 
)

我怎样才能做到这一点?

4

3 回答 3

3

这是因为您要将结果数组添加到新数组中。只需从结果数组中选择您想要的信息并将其放入一个新数组中。

例如:

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

或者:

while($row = mysql_fetch_array($imageResult))
{
    $result_array[] = $row['link'];
}
于 2013-05-01T23:57:52.097 回答
1

逐个元素附加:

$result_array[] = $row[0];
// $result_array[] = $row[1]; this is the one you want to get rid of
于 2013-05-01T23:53:12.063 回答
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 ] )

您在括号中看到它同时表示两者,这告诉它为您提供关联和数字。如果你不想要,你必须指定你想要哪一个。

于 2013-05-01T23:54:49.470 回答