hofan41
09-06-2005, 01:56 AM
I barely see anybody use this technique anymore, so maybe it's completely outdated, but I still find it to be fairly useful.
$array;
// the array you wish to store
$save_string = implode("`|`", $array);
// saves the array as a string where each element is separated by `|`.
//This can be changed to anything else.
use that $save_string to store into a file or database.
to get the array back:
$save_string;
// the string you read from either the file or database.
$array = explode("`|`", $save_string);
// splits $save_string into array elements by removing `|`.
Hope you found this useful. Please note that in this example if the original array contained a string with "`|`" in it, it would not work properly, so you need to escape the characters you use if there is the possibility of that happening.
opera.mp3
09-06-2005, 02:57 PM
I would serialize it:
http://us2.php.net/manual/en/function.serialize.php
LimpBagel
09-26-2005, 01:36 PM
I came onto my current job a few months ago and everything is exploded/imploded so I probably won't rewrite all of it but this serialize function looks very tempting for any future projects.
JustinH
12-26-2005, 10:27 PM
Nobody uses it because it's less than great programming. Fields in a database should only have a single value. If you want to store an array, store it in multiple fields.
hofan41
12-26-2005, 10:56 PM
Nobody uses it because it's less than great programming. Fields in a database should only have a single value. If you want to store an array, store it in multiple fields.
im not sure how exactly you plan on storing an array in multiple fields if the length of the array is uncertain or the array contents are of the same type of data, say a shopping cart's items, or maybe a friends list filled with ID values.
Korvan
12-27-2005, 11:33 AM
thats very easy to do hofan. The way justin would set it up would be
CREATE TABLE cart (
cart_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (cart_id)
);
and for your dynamic data, where you could have an unlimited amount of fields you would put it in another table like this one
CREATE TABLE cart_data (
cart_data_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
cart_id INT UNSIGNED,
cart_data_value varchar(100),
PRIMARY KEY(cart_data_id)
);
so as your array gets bigger you can keep adding more and more values into the cart_data table, when you want to get all the rows of the array back from the database you would query with a left join statement like this
SELECT * FROM cart LEFT JOIN cart_data USING(cart_id) WHERE cart_id = ?
Then you can loop through and restore your array.
The first table would be for static data or data related to the current user, while the second table is only there to fulfill the unknown number of values in your array. If all you are storing is an array, then you can forgo the first table and just use the second one.
hofan41
12-27-2005, 04:25 PM
oh, I see. I misread justin's post and took fields to be actual database fields instead of completely new entries in a database. thanks
JustinH
12-27-2005, 09:20 PM
oh, I see. I misread justin's post and took fields to be actual database fields instead of completely new entries in a database. thanks
Korvan was far more clear than me :D.
trail_blazer
02-06-2006, 11:57 PM
can anyone show me the INSERT statement ?
because i'm not sure how to relate cart id to cart_data_id to identify the array belong to same user
Korvan
02-07-2006, 05:07 PM
no you just compare cart_id, cart_data_id is really moot unless you want to delete a specific cart_data_id field
The first table should contain the information you need to determine who owns the cart, so in your script you should have a variable with the current users cart_id.
So when you insert data you will use "INSERT INTO cart_data (cart_id, cart_data_value) VALUES ({cartid}, '{value}');"
Jamez226
03-19-2006, 02:36 PM
The whole point in databases is that you do not use arrays. You would create a new table with the array info in.