Easy Programming Question. Arrays specifically

I have two arrays coming off a database. They are, for example,

array1=[Dog Dog Cat Cat Fruit Fruit]
array2=[Beagle Pug Garfield Siamese Banana Apple] 

I want to print an out put that is:


There should be an easy way to do this, but for some reason I’m just not seeing it at the moment. The language I am using is PHP if it matters.

something like this:

String strPreviousType = null;
for(int i = 0; i < array2.length; i++)
if(array1* != strPreviousType)
strPreviousType = array1*;

Yep, it was just that easy. Thanks a bunch!


In Perl I’d walk the arrays in the same way as the above, and set up a hash with the key as the animal type and the value the animal instance. It is then a simple matter do dump the output using nested foreach loops without having to check for the type changing.

It always matters.

Yes, I was getting my Pascal code together in my head by the time I read your last sentence… :stuck_out_tongue:

This part is pretty obvious:
B7 02 CC F1 84 29 4F 5D
39 F0 EE 61 78 32 83 D1

Here’s the tricky part:
F7 91 91 A0 0A

edlin? :stuck_out_tongue:

Not really. code_gray’s code wasn’t PHP but it answered my question just fine.

problem is, you have no garuntee that each array matches in order and quantity the members of the other array - and if other code messes with eiether array, the order may also change.

Better to store your array of items in key:value format in a single array/hash/hashmap, then you can loop thru the array, split and sort accordingly, and as voyager states, easily print - without having to be concerned if array1 maps to array2.

also has the benefit of being able to add/subtract from a single object instead of multiple, and never have to rewrite teh print method.

More extendable, and you don’t have to rewrite if the order isnt as expected.

If you did a join on the query then you wold not have two arrays. Or for that matter, just look at how your database is set up. Is there some sort of field in the table with the animals’ names that specifies an object “type”. If not, then you already have a problem is there is an an object with no corresponding entry.

Ok, second question. How do I run select queries on a database without going back to the web server? Basically what I have two menus that trigger functions with an onclick javascript event. What I want to do is do a SELECT (Var 1 & Var 2) and then display that in a 3rd menu. But the only way I know how to do that now is to pass the Vars back to a php function, which I don’t want to do.

You only have 2 choices that I can think of -

[li]bring all the possible data down and store it on the page for JS access on the client[/li][li]go back and get what you need based on user input[/li][/ul]

Without knowing the amount or complexity of any of your data, I can’t say which of the two options is better.

There are plenty of DOJO/JSON and other ‘fancy’ web tricks to do data fetching behind the scenes and rebuild your UI dynamically, but those are still making calls back to the server to get the data, but done properly, these calls can be much less ‘intrusive’ to the client as you only rebuild part of the page, nto the entire thing. (and you can request alot less at any given time - just the data, not the entire page, etc).

Bringing it to the page is going to be much easier. We are talking about 4 fields and maybe 1000 or 2000 rows at the most. I just don’t know how to get it into a form where I can use javascript to run queries on it.

If it were me - I’d use JSON as my data ‘format’ and the JS libraries to read/parse it into usable chunks - if you do a little research into it - should be pretty simple to format your query output into a several JSON objects, and stuff that on your page into things JS vars and go to town.

I’d bet there are even examples of everything out there…

Alright I’ve poked around, and I don’t get what the deal is with json. I don’t see how it gives me the ability to query a database through javascript.

You can’t query a database directly through Javascript running on the client. What you can do is populate a data structure so that you can use Javascript to “look up” data in it.

[scooby doo]


[/scooby doo]

It’s unclear what you are asking at this point. If you are wondering what data manipulation tools exist, I’ll let someone with more experience answer. If you don’t understand what JSON is, then open the following spoiler.

[spoiler]JSON is just an easy way to send arrays and objects from one application to another using plain text strings. You encode your tables as JSON arrays (using the built-in json_encode function of PHP), send them to the client, where the JavaScript can decode them and can be used to look up data.

I don’t really know PHP, but I can give a quick and dirty code example (that is probably crappy code or at least poor practice).

echo "<div id = 'array1' value = '" . json_encode($array1) . "'@gt;</div><div id = 'array2' value='" . json_encode($array2) . "'></div>"

then grab it on the JS side:

var array1 = document.GetElementById('array1');
var array2 = document.GetElementById('array2');
array1 = JSON.parse(array1.value);
array2 = JSON.parse(array1.value);

And now you can do what you want with it. For example, here’s a (again, dirty) JS version of code_grey’s code:

var strPreviousType, output = '';
for(var i = 0; i < array2.length; i++) {
    if(array1* != strPreviousType) {
        output +=  '<b>'  + array1[i] + '</b><br>';
        strPreviousType = array1[i];
    output += array2[i] + "<br>";
} //however you want to output to screen: grab an element, document.write, whatever.


Not really; if it’s just an algorithm you’re after, then knowing the language paradigm is often sufficient. Solving the OP’s problem in a procedural language is different from solving it in a functional language and is different from solving it in a logic programming language. But the basic algorithm is likely to be more or less the same for each respective language paradigm: a solution in C is going to be just as good as a solution in PHP, since they’re both procedural. But a solution in Lisp or Prolog will be much less useful if you want to implement it in PHP.