View Full Version : Advanced Array search and calculate on values

05-22-2008, 10:22 AM
I have converted a PHP Array into a JS Array for performing calculations instead of doing multiple server calls and database queries
the JS Array result is multidimentional and looks like this

Results[0]['RecordID'] = '1';
Results[0]['BranchID'] = '0101';
Results[0]['Controller'] = '4';
Results[0]['Model'] = 'Skoda';
Results[0]['RegDate'] = '10 may 2006';
Results[0]['UnitSold'] = '1';
Results[0]['UnitSold'] = '9000.00';

a small example as there are 600+ numeric keys and 40 associative ones for each numeric
I am looking for an example of a function which could identify values from the ARRAY and return that numeric key so i can access the all the associative ones and peform calculations.

if you look at the example i need to find all occurances of every model. Say 'Skoda' and that it was sold and return the total cost for every result. (Performing calculations)

so if any associative keys match my query return the numeric key it was found in as each numeric key refers to a line of data.
also i have an array of models where i will need to use that to loop through and perform the same caculation for each model and posibly create a new array with the results

NewResults['Skoda']['SalesLast3Months'] = '29';
NewResults['Skoda']['SalesLast1Months'] = '2';
NewResults['Skoda']['CurrentStock'] = '2';
NewResults['Skoda']['TotalValue'] = '12000.05';

NewResults['Polo']['SalesLast3Months'] = '7';
NewResults['Polo']['SalesLast1Months'] = '3';
NewResults['Polo']['CurrentStock'] = '1';
NewResults['Polo']['TotalValue'] = '6050.05';

NewResults['Ka']['SalesLast3Months'] = '12';
NewResults['Ka']['SalesLast1Months'] = '5';
NewResults['Ka']['CurrentStock'] = '6';
NewResults['Ka']['TotalValue'] = '23000.10';


can you help with this?

05-22-2008, 02:15 PM
Hi David,

Here is a brief example as to how I would go about this using multi-dim arrays. My example is less complex than you requested but should give you a starting point perhaps or at least one method for attacking this:

<script type="text/javascript">
aCars = new Array();
aCars[0] = new Array();
aCars[0]['RecordID'] = '1';
aCars[0]['BranchID'] = '0101';
aCars[0]['Controller'] = '4';
aCars[0]['Model'] = 'Skoda';
aCars[0]['RegDate'] = '10 may 2006';
aCars[0]['UnitSold'] = '1';
aCars[0]['Price'] = '9000.00';

aCars[1] = new Array();
aCars[1]['RecordID'] = '2';
aCars[1]['BranchID'] = '0201';
aCars[1]['Controller'] = '4';
aCars[1]['Model'] = 'Skoda';
aCars[1]['RegDate'] = '09 may 2007';
aCars[1]['UnitSold'] = '1';
aCars[1]['Price'] = '4500.00';

function getCarsSoldByMake(make,year){
var total = 0;
for(var i=0;i<aCars.length;i++){
var d = new Date(aCars[i]['RegDate']);
total = total+(aCars[i]['UnitSold']*aCars[i]['Price']);
Sales = new Array();
Sales[make] = new Array();
Sales[make]['TotalSales'] = total;
return total;
As you can see the function getCarsSoldByMake() takes two arguments, a make like 'Skoda' and a year. We only have Skoda in my example but change the year from 2006 to 2007 and you will see that it filters by year so this can be expanded to filter by other date formulas and you can modify it to allow date ranges.

Have you ever done anything with JSON? I would probably use JSON over multi-dim arrays myself, take a look at http://www.json.org/js.html if you are new to JSON.

Going back to the array solution you could also create a function or possibly a class to search on any key and/or value. I'm not sure why you wouldn't want to use PHP/MySQL for this, I would have thought by using SQL and grouping you can reduce the number of queries or another quick idea that entered my tiny brain is to dump the data into a text file that holds the array, so like the JS dumps the array from PHP you would save (basically cache) the array in a text file and then load that then perform all your calculations with PHP as you have alluded to here with JS.

I hope the above has been of some help.

05-22-2008, 02:56 PM
A quick and very basic JSON example:

<script type="text/javascript">
var cars = {"bindings": [
{"Record": "1", "Make": "Ford", "Model": "Focus","Price":"12400","Reg":"20 May 2007"},
{"Record": "2", "Make": "Audi", "Model": "TT","Price":"25000","Reg":"2 June 2008"},
{"Record": "3", "Make": "Ford", "Model": "Fiesta","Price":"300","Reg":"20 April 1995"},
{"Record": "4", "Make": "Toyota", "Model": "Yaris","Price":"10400","Reg":"1 March 2008"},
{"Record": "5", "Make": "Lada", "Model": "Samara","Price":"9000","Reg":"20 May 2007"}
for(var i=0;i<cars.bindings.length;i++){

How to get PHP array to JSON http://www.php.net/manual/en/function.json-encode.php

05-22-2008, 06:14 PM
cheers mate

will check that out

wrapped any chairs around anyones head lately?

05-22-2008, 06:28 PM
cheers mate

will check that out
I know ranjan is the resident JS guru so he may have a better idea of how to go about this than me, it will be interesting to compare methodology.

wrapped any chairs around anyones head lately?
Nope, I'm much more laid back these days, as the years roll on I seem to be able to let more things go without losing my head. I'm 37 next week so it is about time I grew up, even if it is 20 years too late ....lol

05-23-2008, 12:58 AM
I know ranjan is the resident JS guru so he may have a better idea of how to go about this than me, it will be interesting to compare methodology.

Not a guru... Just know enough to get by...

I would have suggested json for porting data simply because they are javascript objects and can be easily handled.

Hence I had no comments after I saw your suggestion.