Given PHP array :
$forColumnsArr['p_pl'] = array('datatype_names' => array('publisher', 'placement', 'placement_groups'), 'labels' => 'All Placements');
$forColumnsArr['a_st'] = array('datatype_names' => array('advertiser', 'campaign', 'strategy'), 'labels' => 'All Campaigns, All Strategies');
Wanted similar/equivalent javascript array
please help me
Represent as objects:
var forColumnsArr = {
p_pl : {
datatype_names : {
0: 'publisher',
1: 'placement',
2: 'placement_groups'
},
labels : 'All Placements'
},
a_st : {
datatype_names : {
0: 'advertiser',
1: 'campaign',
2: 'strategy'
},
labels : 'All Campaigns, All Strategies'
}
}
// console.log(forColumnsArr.p_pl.datatype_names[0]); // publisher
// console.log(forColumnsArr.p_pl.datatype_names[1]); // placement
// console.log(forColumnsArr.p_pl.datatype_names[2]); // placement_groups
I personally prefer to use JS objects
, and you may refer the Mihai's answer for that. However, since you asked for arrays, here you go,
var forColumnsArr['p_pl'] = new Array();
forColumnsArr['p_pl']['datatype_names'][0] = 'publisher';
forColumnsArr['p_pl']['datatype_names'][1] = 'placement';
forColumnsArr['p_pl']['datatype_names'][2] = 'placement_groups';
forColumnsArr['p_pl']['labels'] = 'All Placements';
Similarly, for forColumnsArr['a_st']
, here's the manual
You can generate a JavaScript array from PHP with json_encode
Don't use an array, use an Object. (Well an array in JS is an object, but lets not go there now.)
$forColumnsArr['p_pl'] = array('datatype_names' => array('publisher', 'placement', 'placement_groups'), 'labels' => 'All Placements');
var forColumnsArr = {
'datatype_names': ['publisher', 'placement', 'placement_groups'],
'labels' : 'All Placements'
};
This is called "Object Literal Notation".
Also look up JSON, which is a great timesaver.
var p_pl = {
datatype_names: ['publisher', 'placement', 'placement_groups'],
labels: 'All Placements'
};
var a_st = {
datatype_names: ['advertiser', 'campaign', 'strategy'],
labels: 'All Campaigns, All Strategies'
};
To assign them from php
<?php
$p_pl = json_encode($forColumnsArr['p_pl']);
$a_st = json_encode($forColumnsArr['a_st']);
?>
var p_pl = eval('(' + '<?php echo $p_pl; ?>' + ')');
var a_st = eval('(' + '<?php echo $a_st; ?>' + ')');
var forColumnsArr = {
p_pl: {
datatype_names: {
0:'publisher',
1: 'placement',
2: 'placement_groups'
},
labels: 'All Placements'
},
a_st: {
datatype_names: {
0: 'advertiser',
1: 'campaign',
2: 'strategy'
},
labels: 'All Campaigns, All Strategies'
}
};
Use object- and array-literals:
var forColumnsArr = {
p_pl: {
datatype_names : [
'publisher',
'placement',
'placement_groups'
],
labels : 'All Placements'
},
a_st: {
datatype_names: [
'advertiser',
'campaign',
'strategy'
],
labels: 'All Campaigns, All Strategies'
}
};
Accessing the properties of those objects works just like in PHP, either with bracket or dot notation.
You can easily generate this JSON markup by calling the json_encode
function on your PHP object.
var object = {
'datatype_names' : {
'publisher':'Value',
'placement':'Value',
'placement_groups':'Value'
},
'labels' : 'All Placements'
}
console.log(object);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.