简体   繁体   English

是否有用PHP创建数组的简写

[英]Is there shorthand to create an array in PHP

After I get the result from the recordset, I'm shortening the name for easier writing in the page. 从记录集获得结果后,我将缩短名称,以便于在页面中编写。 I'm echoing $TitleTarg[1] thru $TitleTarg[100]. 我通过$ TitleTarg [100]回显$ TitleTarg [1]。 It's all working, but is there a shorthand for this so I don't have to write it 100 times? 一切正常,但是是否有速记形式,所以我不必写100次? It's used in many pages. 它在许多页面中使用。

$TitleTarg = array(
'1'=>$row_rsTargTitles['description1'],
'2'=>$row_rsTargTitles['description2'],
'3'=>$row_rsTargTitles['description3'],
'4'=>$row_rsTargTitles['description4'],
'5'=>$row_rsTargTitles['description5],

ad nauseum to 100
);

I've been reading for three hours and checked the 'Similar Questions' section to no avail. 我已经阅读了三个小时,没有检查“类似问题”部分。 Working answers are appreciated and up-voted. 工作的答案是赞赏和投票。

Thanks for helping. 感谢您的帮助。

Since your source's keys are always descriptionN with N being a number counted upward without skipping one, this could easily be done with a simple for loop like this 由于您的源代码的密钥始终是descriptionN其中N是一个向上计数的数字而不跳过一个数字,因此可以使用一个简单的for循环轻松完成

$TitleTarg = array();
for ($i=1; $i<=100; $i++) {
    $TitleTarg[$i] = $row_rsTargTitles['description'.$i];
}

Make sure you have the correct limits (start and end). 确保您具有正确的限制(开始和结束)。

you can achive this through easy code 您可以通过简单的代码来实现

for ($i=1; $i<=100; $i++) 
{ 
    $temp = 'description'.$i;
    $TitleTarg[$i] = '$row_rsTargTitles['.$temp.']'; 
}

echo "<pre>";
print_r($TitleTarg);

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

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