0

First time posting here after having great results searching for other answers on stackoverflow. I've hit a roadblock in an app I'm trying to build. It involves plotting a graph with JS using dynamic values pulled from a database.

I'm at the point now where I have all the values I need to plot a graph using a javascript library. The basic requirement to plot a graph is a basic array that looks something like this:

var plot1 = [ [2,15], [4,23], [5,25] ];

In my case of [x,y] x = date and y = clicks. With that data, you could call the function like so:

$.plot($("#placeholder"), [ plot1 ];

This does all the work for you and builds a nice little graph. In my case I do all of this dynamically. Here's where I'm stuck. I have an array that outputs this when printed:

Array ( 
    [0] => var test7 = [[1, 1]]; 
    [1] => var test2 = [[1, 1]]; 
    [2] => var test2 = [[4, 1]]; 
    [3] => var test2 = [[15, 2]]; 
    [4] => var test1 = [[2, 1]]; 
    [5] => var test1 = [[6, 7]]; 
    [6] => var test1 = [[14, 1]]; 
    [7] => var test1 = [[15, 1]]; 
    [8] => var giver = [[3, 2]]; 
    [9] => var giver = [[14, 1]]; 
    [10] => var test4 = [[4, 1]]; 
);

What I need to do now, is group all of the values that contain the same linkId (test2 has 3 instances for example) together into one line. Here's what I need the array to look like:

Array ( 
    [0] => var test7 = [[1, 1]]; 
    [1] => var test2 = [ [1, 1], [4, 1], [15, 2] ]; 
    [4] => var test1 = [ [2, 1], [6, 7], [14, 1], [15, 1] ]; 
    [8] => var giver = [ [3, 2], [14, 1] ]; 
    [10] => var test4 = [[4, 1]]; 
);

From that point I can run a foreach on it an echo out the values inside a script tag, and we're up and running!

Suggestions? I know there isn't going to be a single built in function that accomplishes everything I need in one quick swoop. array_unique() is close but not quite what I need. I think I can use array_walk($array, callback) which accepts an array and a callback function that is executed on each value within the array, but I'm really not sure where to go from there.

Any help is greatly appreciated!

sagalbot
  • 118
  • 4
  • is each value in the array a string with the text `"var testN = [[X,Y]];"`? I only ask because it seems to me like in php it should be structured a little different. For example is that formatted from the result of a query? if so then it would probably be easier to structure the data differently to make the output easier to format. As in a simple `json_encode()` on the array and then use javascript to loop through. – Jonathan Kuhn Feb 08 '11 at 00:36
  • @Jonathan - Yeah, each value contains a string. It took me about 80 lines just to get to that point. I'm sure it's not the most efficient process, but I'll improve it over time. Thanks for your input. – sagalbot Feb 08 '11 at 02:07

2 Answers2

2
$array = array( 
  'var test7 = [[1, 1]];', 
  'var test2 = [[1, 1]];', 
  'var test2 = [[4, 1]];', 
  'var test2 = [[15, 2]];', 
  'var test1 = [[2, 1]];', 
  'var test1 = [[6, 7]];', 
  'var test1 = [[14, 1]];', 
  'var test1 = [[15, 1]];', 
  'var giver = [[3, 2]];', 
  'var giver = [[14, 1]];', 
  'var test4 = [[4, 1]];' 
);

foreach ($array as $val) {
  preg_match('~var ([^ ]+) = \[\[([^\]]+)~',$val,$var);
  $tArray[$var[1]][] = "[".$var[2]."]";
}
$array = array();
foreach ($tArray as $key => $val) {
  $array[] = 'var '.$key.' = ['. implode(',',$val) . '];';
}
CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
0

You know you can use strings as array indexes in Javascript right?

So you could do something like... (pseudo code btw)

final = array();
final[arrayWithData->Key] += arrayWithData->value
Nick
  • 3,096
  • 3
  • 20
  • 25