[英]Can I create array using while loop
================================================================================== ================================================== ================================
Solution Code: 解决方案代码
$array = array();
$i = 1;
while( $i <= $instance['posts_num'] ) {
$array['tab'.$i.'_title'] = 'Category Heading';
$array['tab'.$i.'_thumb'] = 'Insert link here';
$array['tab'.$i.'_featured'] = 'Insert link here';
$array['tab'.$i.'_description'] = 'Insert category desciption';
$array['tab'.$i.'_link'] = 'Insert category link';
$i += 1;
}
$instance = wp_parse_args( (array)$instance, array(
'heading' => 'Featured Area',
'title' => '',
'posts_num' => 1,
(array) $array
) );
================================================================================== ================================================== ================================
how can I create array with while loop. 如何使用while循环创建数组。
I am trying to create an array with the help of while loop in simple wordpress plugin. 我试图在简单的wordpress插件的while循环的帮助下创建一个数组。 my codes are below: 我的代码如下:
$instance = wp_parse_args( (array)$instance, array(
'heading' => 'Featured Area',
'title' => '',
'posts_num' => 1,
$array = array();
$i = 1;
while( $i <= $instance['posts_num'] ) {
$array[] = 'tab'.$i.'_title' => 'Category Heading',
$array[] = 'tab'.$i.'_thumb' => 'Insert link here',
$array[] = 'tab'.$i.'_featured' => 'Insert link here',
$array[] = 'tab'.$i.'_description' => 'Insert category desciption',
$array[] = 'tab'.$i.'_link' => 'Insert category link',
$i += 1;
}
) );
I just want to confirm if i am doing correctly. 我只是想确认一下我是否做得正确。
I want to generate array like this : 我想生成这样的数组:
'tab1_title' => 'Category Heading',
'tab1_thumb' => 'Insert link here',
'tab1_featured' => 'Insert link here',
'tab1_description' => 'Insert category desciption',
'tab1_link' => 'Insert category link',
'tab2_title' => 'Category Heading',
'tab2_thumb' => 'Insert link here',
'tab2_featured' => 'Insert link here',
'tab2_description' => 'Insert category desciption',
'tab2_link' => 'Insert category link',
You're trying to put a while loop inside array()
. 你试图在array()
放入一个while循环。 That's just not going to work. 这不会起作用。 Construct the array before the call to wp_parse_args
and then pass your finished array in. 在调用wp_parse_args
之前构造数组,然后将完成的数组传递给。
while( $i <= $instance['posts_num'] ) {
$array["tab{$i}_title"] = 'Category Heading';
//...Repeat for others here.
$i += 1;
}
array(4) {
["tab0_title"]=>
string(16) "Category Heading"
["tab1_title"]=>
string(16) "Category Heading"
["tab2_title"]=>
string(16) "Category Heading"
["tab3_title"]=>
string(16) "Category Heading"
}
The problem with your code is that you are using a wrong syntax/logic. 代码的问题在于您使用了错误的语法/逻辑。
Example : 示例:
$array['tab'.$i.'_title'] = 'Category Heading';
$array['tab'.$i.'_thumb'] = 'Insert link here';
$array['tab'.$i.'_featured'] = 'Insert link here';
$array['tab'.$i.'_description'] = 'Insert category desciption';
$array['tab'.$i.'_link'] = 'Insert category link';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.