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
https://forums.phpfreaks.com/topic/136977-data-array-help/
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
https://forums.phpfreaks.com/topic/136977-data-array-help/#findComment-715456
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
https://forums.phpfreaks.com/topic/136977-data-array-help/#findComment-715457
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
https://forums.phpfreaks.com/topic/136977-data-array-help/#findComment-715469
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
https://forums.phpfreaks.com/topic/136977-data-array-help/#findComment-715506
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.