Jump to content

Data array help


Imad

Recommended Posts

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.