Imad Posted December 15, 2008 Share Posted December 15, 2008 Hi guys, I'm trying to grab data from a table in a database and make it into an array. I've achieved such, but the array is in this format using the explode function: array( [0] => word [1] => words [2] => etc ); How can I get it to an array like this: array('word','words','etc'); Thanks in advanced. Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted December 15, 2008 Share Posted December 15, 2008 I think you'll find that the two are synonymous. All array entries have both a key and a value. The first format is that used when displaying an array using the print_r() function. The numeric parts [0], [1] and [2] are the keys for each entry. The second display is how people typically define arrays in code, allowing PHP to autoallocate the keys for each entry, strating from 0 and incrementing by 1 for each additional entry. Quote Link to comment Share on other sites More sharing options...
tarlejh Posted December 15, 2008 Share Posted December 15, 2008 Hi guys, I'm trying to grab data from a table in a database and make it into an array. I've achieved such, but the array is in this format using the explode function: array( [0] => word [1] => words [2] => etc ); How can I get it to an array like this: array('word','words','etc'); Thanks in advanced. Those two arrays are identical. An array is always a set of key/value pairs. The keys can be numeric or associative. If you don't explicitly set the keys, and just do: array('x', 'y', 'z') PHP assigns a key of 0 to x, a key of 1 to y, etc. HTH Quote Link to comment Share on other sites More sharing options...
Imad Posted December 15, 2008 Author Share Posted December 15, 2008 Thanks for your replies. The problem is that it won't work for my foreach statement. Here's a brief look at it: foreach ($words as $word) { $lev = levenshtein($input, $word); .... $words would be the array, if I have the array with the keys assigned, I get an error, without them, I don't. Any ideas why? Best Regards. Quote Link to comment Share on other sites More sharing options...
Imad Posted December 15, 2008 Author Share Posted December 15, 2008 I figured out a workaround. I basically grabbed the data from the database, and instead of using the explode function, I used the str_replace function and made it add an apostrophe, then a word then another apostrophe and a comma for each of the words. I then put the variable in an array, and viola, an array without specified keys. Thanks for everyone's help, Best Regards. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.