简体   繁体   English

动态PHP数组结构

[英]Dynamic PHP array structure

So I want to get all of the links of the images in my database: 因此,我想获取数据库中图像的所有链接:

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

The print_r(); print_r(); returns this: 返回此:

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

I'm looking for something similar to: 我正在寻找类似的东西:

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 
)

How can I do that? 我怎样才能做到这一点?

Its because you are adding the result array to a new array. 这是因为要将结果数组添加到新数组。 Simply select the information you want from the result array and place that in a new array. 只需从结果数组中选择所需的信息,然后将其放置在新数组中即可。

For example: 例如:

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

OR: 要么:

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

Attach element by element: 逐个元素附加:

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

Specify you just want numeric, not both: 指定您只想要数字,而不是两者都:

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

or if you are on an older version of php: 或者,如果您使用的是旧版本的php:

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

The default is: 默认值为:

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

You see in brackets it says both, which tells it to give you both associative and numeric. 您会在方括号中看到这两个字,并告诉它既给您联想又给您数字。 You have to specify which one you want if you do not want that. 如果不想,则必须指定想要的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM