0

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

Manojkumar
  • 1,351
  • 5
  • 35
  • 63

8 Answers8

1

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
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
1

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

Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
  • @Baba: It is valid JS, though not working (missing an object) and much too PHP-like. An object/array literal would be better. – Bergi Oct 09 '12 at 14:08
0

You can generate a JavaScript array from PHP with json_encode

Matt
  • 9,068
  • 12
  • 64
  • 84
0

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.

Erwin Moller
  • 2,375
  • 14
  • 22
0
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; ?>' + ')');
air4x
  • 5,618
  • 1
  • 23
  • 36
0
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'
    }
};
Riz
  • 9,703
  • 8
  • 38
  • 54
0

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0
var object = {
    'datatype_names' : {
        'publisher':'Value', 
        'placement':'Value', 
        'placement_groups':'Value'
    },
    'labels' : 'All Placements'
}

console.log(object);
Coreus
  • 5,360
  • 3
  • 35
  • 50