动态PHP数组结构

时间:2013-05-01 23:51:31

标签: php mysql arrays

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

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

我该怎么做?

3 个答案:

答案 0 :(得分:3)

因为您要将结果数组添加到新数组中。只需从结果数组中选择所需的信息,然后将其放入新数组中即可。

例如:

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

OR:

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

答案 1 :(得分:1)

按元素附加元素:

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

答案 2 :(得分: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 ] )

你在括号中看到它们都表示,它告诉它给你联想和数字。如果你不想要,你必须指定你想要的那个。