admin管理员组文章数量:1122832
I have following code in my plugin of Wordpress:
wp_localize_script('ffd_js_script', 'myAjax', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'idinfo' => $myoptionValue[idinfo],
'index1' => $myoptionValue[id1],
'index2' => $myoptionValue[id2]
)
);
I want to replace
'index1' => $myoptionValue[id1],
'index2' => $myoptionValue[id2]
with
for($i=1; $i<= $myoptionValue[fieldcount]; $i++)
{
$arguments .= ',"index"'.$i.'=>'.$myoptionValue[id.$i];
}
So that I have
wp_localize_script('ffd_js_script', 'myAjax', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'idinfo' => $myoptionValue[idinfo]
$arguments
)
);
Apparently I'm thinking it might be as simple as this, but it isn't, where is my mistake?
EDIT:
full code:
require( plugin_dir_path( __FILE__ ) . 'menu.php');
require_once( plugin_dir_path( __FILE__ ) . 'getuser.php');
add_action( 'wp_enqueue_scripts', 'ffd_load_scripts' );
function ffd_load_scripts()
{
$myoption = get_option( 'fixformdata_options' );
$myoptionValue = maybe_unserialize( $myoption );
$arguments = array();
for($i=1; $i<= $myoptionValue[fieldcount]; $i++)
{
$arguments['index'.$i] = $myoptionValue['id'.$i];
}
wp_register_script('ffd_js_script', WP_PLUGIN_URL.'/FixFormData/js/ffd_js_script.js', array('jquery'));
wp_localize_script('ffd_js_script', 'myAjax', merge_array(array(
'ajaxurl' => admin_url('admin-ajax.php'),
'idinfo' => $myoptionValue['idinfo']),$arguments)
);
wp_enqueue_script('jquery');
wp_enqueue_script('ffd_js_script', plugin_dir_url(__FILE__) . 'js/ffd_js_script.js');
}
I have following code in my plugin of Wordpress:
wp_localize_script('ffd_js_script', 'myAjax', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'idinfo' => $myoptionValue[idinfo],
'index1' => $myoptionValue[id1],
'index2' => $myoptionValue[id2]
)
);
I want to replace
'index1' => $myoptionValue[id1],
'index2' => $myoptionValue[id2]
with
for($i=1; $i<= $myoptionValue[fieldcount]; $i++)
{
$arguments .= ',"index"'.$i.'=>'.$myoptionValue[id.$i];
}
So that I have
wp_localize_script('ffd_js_script', 'myAjax', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'idinfo' => $myoptionValue[idinfo]
$arguments
)
);
Apparently I'm thinking it might be as simple as this, but it isn't, where is my mistake?
EDIT:
full code:
require( plugin_dir_path( __FILE__ ) . 'menu.php');
require_once( plugin_dir_path( __FILE__ ) . 'getuser.php');
add_action( 'wp_enqueue_scripts', 'ffd_load_scripts' );
function ffd_load_scripts()
{
$myoption = get_option( 'fixformdata_options' );
$myoptionValue = maybe_unserialize( $myoption );
$arguments = array();
for($i=1; $i<= $myoptionValue[fieldcount]; $i++)
{
$arguments['index'.$i] = $myoptionValue['id'.$i];
}
wp_register_script('ffd_js_script', WP_PLUGIN_URL.'/FixFormData/js/ffd_js_script.js', array('jquery'));
wp_localize_script('ffd_js_script', 'myAjax', merge_array(array(
'ajaxurl' => admin_url('admin-ajax.php'),
'idinfo' => $myoptionValue['idinfo']),$arguments)
);
wp_enqueue_script('jquery');
wp_enqueue_script('ffd_js_script', plugin_dir_url(__FILE__) . 'js/ffd_js_script.js');
}
Share Improve this question edited Aug 30, 2014 at 21:27 stijn.aerts asked Aug 30, 2014 at 19:15 stijn.aertsstijn.aerts 1131 gold badge1 silver badge9 bronze badges 9 | Show 4 more comments1 Answer
Reset to default 0There are 4 valid ways of defining a string, see this question for more details.
However in your case there is a special exception provided by PHP. Most users are unaware of it however, and it does leave ambiguities, as it's no longer possible to tell a string literal, a constant, or a define apart, making it very difficult for other programmers to read your code ( e.g. yourself in 9 months time ).
So your code:
for($i=1; $i<= $myoptionValue[fieldcount]; $i++)
{
$arguments .= ',"index"'.$i.'=>'.$myoptionValue[id.$i];
}
Is trying to append a string to the end of an array, which doesn't work. Arrays aren't strings, and you can only use the .=
and .
operators on strings. Also $myoptionValue[id.$i]
violates the special case situation as the expression is now ambiguous ( does it mean 'id.$i', 'id'.$i, or "id.$i"? )
To add an item to an array you need to do one of the following:
$arr = array();
$arr['key'] = 'value';
$arr[] = 'value without a key';
array_push( $arr, 'value1', 'value2', 'value3', etc... );
So your loop should be:
for ( $i=1; $i <= $myoptionValue['fieldcount']; $i++ ) {
$arguments['index'.$i] = $myoptionValue['id'.$i ];
}
本文标签: Replacing string with a variable in php
版权声明:本文标题:Replacing string with a variable in php 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290683a1928565.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$myoptionValue[id2]
also looks like it would generate a PHP fatal error, do you mean$myoptionValue[$id2]
? To truly condense this down you need to show more of the surrounding code – Tom J Nowell ♦ Commented Aug 30, 2014 at 19:29idinfo
,id1
andid2
defined? Unless these are constants somewhere this is invalid PHP, I would expect your original code to generate fatal errors – Tom J Nowell ♦ Commented Aug 30, 2014 at 19:42