1

If I have an array like so:

var phpintojsArray = [["1","20"]];

and... I also have a multid-array:

var data = [[1,20], [4, 20], [7, 55], [9, 10], [9, 10]];

how to add the phpintojsArray into the data array? I want:

var data = [[phpintojsArray], [1,20], [4, 20], [7, 55], [9, 10], [9, 10]];

What is one way to accomplish this?

Clarification -- Trying to manipulate the data in a chart

This works:

    /* Bar Chart */
    var data = [[1, 10],[3, 60], [5, 20], [7, 50], [9, 10]];

    // Initialize Bars Chart
    $.plot(BarChart, [
        { data: data, bars: { show: true, fillColor: { colors: [{ opacity: 1 }, { opacity: 1 }] } }, label: 'Example label' } ],
        {
            legend: {
                backgroundColor: '#f6f6f6',
                backgroundOpacity: 0.8
            },
            colors: ['#39d5db'],
            grid: {
                borderColor: '#cccccc',
                color: '#999999',
                labelMargin: 10
            },
            yaxis: {
                ticks: 5
            },
            xaxis: {
                tickSize: 1
            }
        }
    );

This does Not work:

var phpintojsArray = <?= json_encode($sales); ?>;
    var two = [3, 60];
    var three = [5, 20];
    var four = [7, 50];        
    var five = [9, 10];


    /* Bars Chart */
    var data = [[phpintojsArray], [two], [three], [four], [five]];

    // Initialize Bars Chart
    $.plot(BarChart, [
        { data: data, bars: { show: true, fillColor: { colors: [{ opacity: 1 }, { opacity: 1 }] } }, label: 'Example label' } ],
        {
            legend: {
                backgroundColor: '#f6f6f6',
                backgroundOpacity: 0.8
            },
            colors: ['#39d5db'],
            grid: {
                borderColor: '#cccccc',
                color: '#999999',
                labelMargin: 10
            },
            yaxis: {
                ticks: 5
            },
            xaxis: {
                tickSize: 1
            }
        }
    );

Why?

6
  • what you have doesn't work? Commented May 16, 2014 at 1:59
  • Nope. When i view the source it shows the name "phpintojsArray" not the value Commented May 16, 2014 at 2:02
  • Yeah, it will show phpintojsArray with the code above. View source is not a good indicator of whether something worked... Commented May 16, 2014 at 2:04
  • View source is viewing source - it's like looking at what you have typed above literally and saying "my code doesn't work" - as it turns out, your code does work: jsfiddle.net/sLp9B Commented May 16, 2014 at 2:05
  • I think you're looking for data = phpintojsArray.map(Number).concat(data); actually. Commented May 16, 2014 at 2:13

3 Answers 3

2

You can use splice to insert the elements:

data.splice(0, 0, phpintojsArray);
Sign up to request clarification or add additional context in comments.

4 Comments

Does this do insert the element of phpintojsArray or the whole phpintojsArray itself?
This will add whole phpintojsArray itself
One more question , what do the param 0,0 mean here , I know the first is an index.
The syntax of the splice method is array.splice(index,howmany,item1,.....,itemX) where the first param is the index, second param is how many elements to be deleted and the rest are the new elements to be added
1

This works:

/* Bar Chart */  
var data = [[1, 10],[3, 60], [5, 20], [7, 50], [9, 10]];

This does Not work:

/* Bars Chart */
var data = [[phpintojsArray], [two], [three], [four], [five]];

Why?

Because the two expressions yield different structures. In the first one you have two levels of nested arrays (small arrays inside a big one). In the second you have three levels of nested arrays.

A simple console.log will show you the difference.

Make sure that your data is in the correct structure. Change it to:

var data = [two, three, four, five];

(In case it's still not clear, the brackets [] define a new array)

Regarding your question on how to prepend phpintojsArray, you could do:

Array.prototype.unshift.apply(data, phpintojsArray);

3 Comments

understood thanks. regarding Array.prototype.unshift.apply(data, phpintojsArray); how would you end up using that inside the data array?
what do you mean by 'inside'?
Meaning, how to use your prepending technique to get the value of phpintojsArray and use it inside of the data array (like below:) var data = [[Array.prototype.unshift.apply(data, phpintojsArray);], [two], [three], [four], [five]]; Because your point about the different structures works for the var data = [[two], [three], [four], [five]]; but not for the phpintojsArray value: var data = [[phpintojsArray], [two], [three], [four], [five]];[phpintojsArray].
0

You could use eval in javascript.

For Example:

var data = [[eval("phpintojsArray")], [eval("two")], [eval("three")], [eval("four")], [eval("five")]];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.