|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
building a table into an array
Hi everyone,
I am writing some code that will go through a chunk of text and compares each word in the text to the contents of a table (glossary table with term and definition). When it finds a word in the glossary table it incases it with some javascript that will display the definition. i got this to work using a nested loops but it was running a SELECT on the table for every word and therefore taking ages. what i want to do is build the glossary table into an array once and then search one array against the other. my question: whats the easiest way of doing this? thanks in advance toby |
|
#2
|
|||
|
|||
|
Building an array in PHP is very easy.
If you have an array called $glossary, then all you need to do inside a loop: foreach ( $data as $glossary_word ) { $glossary[] = $glossary_word; }
__________________
__________________________________________________ _ Wil Moore III, MCP | Integrations Specialist | Senior Consultant Are You Listed...? | DigitallySmooth Inc. |
|
#3
|
|||
|
|||
|
Sorry reading back my message i wasn't clear at all. I want to build an array that holds both the term and the definition, ie
foreach ( $data as $glossary_word ) { $glossary[][] = $glossary_term , $glossary_definition; } I haven't tried the code above and it is a guess. I know two dimentional arrays exsit but not how to create one. |
|
#4
|
|||
|
|||
|
In this case you would do the following (assuming you have a database recordset):
PHP Code:
|
|
#5
|
|||
|
|||
|
That looks pretty good to me. Can i trouble you for a bit more time and ask for a short explaination of whats going on? well actually if i say what i think it happening
PHP Code:
$rs is the recordset $glossary is our 2 dim array containing the term and the definition I can loop through a normal array, what do i need to do to output it. one more thing... can i use inarray() function on a 2 dimentional array? thanks again |
|
#6
|
|||
|
|||
|
Actually here are a few facts:
1. What we just looked at is not really a two dimensional array. It is still one dimension, however, it is not indexed as you would normally imagine. It is called an associative array. Normal arrays are indexed by number, such as: $element[0], $element[1]... [n] 2. To Print this array's elements to screen you would use: PHP Code:
3. The same works for an associative array. Basically the only difference is that an associative array is index by a string instead of numbers. 4. Yes, $rs is the recordset 5. $glossary is our an associative array containing the term (as the key) and the definition (as the value). 6. in_array() would actually search through the values of the array, not the keys. use array_key_exists() instead |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > building a table into an array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|