PHP: Replace duplicate instances of string within array, more complex than it sounds

433 views Asked by At

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!

2

There are 2 answers

1
CrayonViolent On BEST ANSWER
$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) . '];';
}
0
Nick On

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