开发者

PHP array in a query

开发者 https://www.devze.com 2023-03-23 17:15 出处:网络
Is there any way, to make something like: $elements_string = array() foreach ($something as $element => $value ) {

Is there any way, to make something like:

$elements_string = array()
foreach ($something as $element => $value ) {
$elements_string[$element] = $value;
...
}

And save $elements_string into a db row?

Maybe you guys prefer some other way?

I wan't to read it from the database so I can easly parse the string.

Earlier I was using something similar to:

$elements_string = $element . ":" . $value;

And I was trying to use explode on that, but that makes no sense because I need to hav开发者_高级运维e $element and $value in one loop for one element.


Normally you would store each element and value in a different row.

If you must store them in one row (and be sure you really have to do that), then use serialize() http://www.php.net/serialize to turn an array into a string you can store.

You can do other things to like: value:element;value:element - then you first split by semicolon, then by colon.


You could use implode(glue,pieces) to turn an array into a string with a given separator, supposing you have a character you never use. (and explode(delimiter,string) to get it back)

Serializing may be a better option in most cases, though. Use serialize(value) to store it and unserialize(value) to get it back.

0

精彩评论

暂无评论...
验证码 换一张
取 消