Re: Storing an array on a table?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Yes, this has been mentioned in this thread. But with serialize/ unserialize, you can run into other problems that may be more confusing/difficult to troubleshoot. e.g.:

http://www.php.net/serialize

>As you can see, the original array :
>$arr["20041001103319"] = "test"
>
>after serialize/unserialize is:
>$arr[683700183] = "test"

yepp, and i can explain it.

the internal compiler of php does not hit anys rule wich foces him to make that number a string during serialisation. since it becomes an integer and php supports 32bit interger not arbitary bitwidth this is what happens:

"20041001103319"
equals hexadecimal:

0x123A28C06FD7h

if you cut away the frontpart cutting down to 32bis,
you get:

0x28C06FD7h

wich equals 683700183.


For simple arrays, I prefer storing everything as a simple imploded string. YMMV.

Jordan





On Sep 1, 2005, at 10:18 AM, <tg-php@xxxxxxxxxxxxxxxxxxxxxx> <tg- php@xxxxxxxxxxxxxxxxxxxxxx> wrote:

Sorry, didn't catch this thread from the beginning, but did anyone recommend trying the serialize() and unserialize() commands? They'll convert the array to a block of text that can be stored, retrieved and unserialized.

My gut instinct is that if you're trying to store any array in a database, you may not have thought through your design very well. BUT.. I also know that there are cases where you might want to (I've actually done it before... being lazy in that case..hah) so dont take that as criticism, just wondering if there's a "more right" way to do it.

If that's what you need to do though, definitely check out serialize (unless someone knows something I don't).

Serialize() should do essentially what's being proposed below, just without having to figure out what string may not be in your array.

good luck!

-TG


= = = Original message = = =

if you just have a simple array with automatic numeric keys and text
you could just implode the data to a string with a separator not
found in your data:
$dataArray = array("hello", "goodbye", "etc.");
$storable = implode("||||", $dataArray);
// $storable becomes "hello||||goodbye||||etc."

//then, use explode to get the original array back again
$dataArray = explode("||||", $storable);

you could use a similar technique if you want to put the keys in as
well, albeit slightly more complicated (e.g. use "||||" to separate
each element in the array and "||" to separate each key from its
value). Just find a divider you know your data will not contain, such
as a pipe: "|".

This has worked well for me.

Jordan



On Sep 1, 2005, at 8:55 AM, Miguel Guirao wrote:




I want to store an array into a field on a MySQL table, Is it
posible to
save it? Maybe into a string field?





___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux