使用计数器作为变量的一部分 [英] Use counter as part of variable

查看:98
本文介绍了使用计数器作为变量的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想做的

for($i = 1; $i < 4; $i++) {

    $featuredImage$i = $featured['featuredImage$i'];
    $featuredText$i  = $featured['featuredText$i'];
    $featuredURL$i   = $featured['featuredURL$i'];

    }

我该怎么办?

推荐答案

您只需要学习PHP语言。

此页面包含您需要的一切: http://php.net/types.string

不管这个特殊问题,都值得一读语法是最基本的PHP语法概念之一,无论如何,您都必须认真学习。

Only thing you need is to learn PHP language.
This page contains everything you need: http://php.net/types.string
It worth reading regardless this particular question, as string syntax is one of most basic PHP syntax concepts and you have to learn it by heart anyway.

我唯一要补充的一点是:严格来说,您不要将计数器用作 variable 的一部分,而不用作 string 的一部分,而这又将成为数组变量的键。但是从语法上讲,您是在字符串和变量之间组成一个字符串。

The only note I have to add: strictly speaking, you're gonna use a counter not as a part of variable but as a part of string, which, in it's turn, gonna be a key for the array variable. But syntactically you're composing a string out of a string and a variable.

编辑

哦。我没注意到最左边的变量。

只有当我开始写另一个建议时,我才能够看到它。

Oh. I didn't notice leftmost variable.
Only when I started writing another suggestion, I was able to see it.

嗯,你可能也考虑使用嵌套数组。

可以用这种方式

Well, you may also consider using nested array.
It could be used this way

foreach ($featured as $item) {

    echo $item['featuredImage'];
    echo $item['featuredText'];
    echo $item['featuredURL'];
}

或对这些变量进行任何操作。

or do whatever you want to do with these variables.

很高兴看到导致您遇到此问题的完整上下文,因此有可能为您的特定情况提供最佳解决方案。

您将需要极大地重新设计数据库...

It would be nice to see the full context which lead you to this question, thus it will be possible to give you best solution for your particular case.
May be you will need dramatically redesign your database...

这篇关于使用计数器作为变量的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆