简体   繁体   English

php concating变量和访问数组

[英]php concating variable and accessing array

is somthing like that possible? 有可能吗?

for ($i = 1; $i <= 15; $i++) {
        $var = "bild" . $i;
        if (!empty ($row->bild.$i)) {
            echo '
                <div class="item">
                        <img border="no" class="bildschatten" src="include/images/projekte/'.$row->bild . $i.'" />
                </div>
            ';
        }
    }

how should it look correctly? 怎么看起来正确? problem is: 问题是:

I fetch objects from a mysql database, in this table each object has 15 images 我从mysql数据库中获取对象,在这个表中每个对象有15个图像

bild1 .. bild15 bild1 .. bild15

Now I want to iterate over this 15 images and want to check if the colum is empty or not. 现在我想迭代这15张图片,并想检查列是否为空。

the problem I have is here: 我遇到的问题是:

$row->bild.$i

that contains not the value of column bild1.. bild15 ... it only contains the value of $i 它不包含列bild1的值.bild15 ...它只包含$ i的值

thanks 谢谢

You could fetch the result as an array uning 您可以将结果作为数组uning获取

    $row  =  db_fetch_array($result) 

and then accessing 然后访问

    $var = "bild" . $i;
    if (!empty ($row[$var])) {

This is called variable of variables, You need to access something like this $row->$var . 这被称为变量变量,你需要访问类似$row->$var

Complete Solution: 完整解决方案

for ($i = 1; $i <= 15; $i++) {
    $var = "bild" . $i;
    if (!empty ($row->$var)) {
        echo '
            <div class="item">
                    <img border="no" class="bildschatten" src="include/images/projekte/'.$row->$var.'" />
            </div>
        ';
    }
}

For more details checkout the Manual . 有关详细信息,请查看手册

这段代码应该有效:

$row->{'bild'.$i}

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

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