[PHP 5.5] String to array conversions

Experienced Elementalist
Joined
Jul 23, 2012
Messages
201
Reaction score
131
Hello, i will start with basic introduction, lol, so i am storing data in database array items as string for example,

Table example

ID | NAME | ARRAY_STRINGS

ID = 1
NAME = something
1.ARRAY_STRINGS = MyItem,MyItem2

it can get more complicated like this

2.ARRAY_STRINGS = MyKey:MyItem,MyKey2:MyItem2
3.ARRAY_STRINGS = MyKey:MyItem,MyKey2:MyItem2,MyITem3
4.ARRAY_STRINGS = MyKey:MyItem
5.ARRAY_STRINGS = MyItem

and so on, so example would be converted to
PHP:
1. array(MyItem,MyItem2);
2. array(MyKey=>MyItem,MyKey2=>MyItem2);
3. array(MyKey=>MyItem,MyKey2=>MyItem2,MyITem3=>MyITem3);
4. array(MyKey=>MyItem);
5. array(MyItem);

then these arrays are cached to php file and saved for latter usage.. the php code:

[paste]dHfajpPv[/paste]

So is there any suggestion to improve this code, because at one point i go 4 loops deep, so there is an design error in the code, idk if the code is working i write it on the fly, i just need some pointers, thanks

EDIT

Solved this, found good article on this subject, can be closed
 
Last edited:
Although you've said you found a solution for this, I'd personally use serialize and unserialize, the data stored under the 'ARRAY_STRINGS' part of your database can be serialized, when you unserialize them you'd have an array you can use in PHP without having to write a huge amount of code.

PHP:
1. array(MyItem,MyItem2);
2. array(MyKey=>MyItem,MyKey2=>MyItem2);
3. array(MyKey=>MyItem,MyKey2=>MyItem2,MyITem3=>MyITem3);
4. array(MyKey=>MyItem);
5. array(MyItem);

When using the above arrays as only the 'ARRAY_STRINGS' part this method would work with very minimal effort.
 
Although you've said you found a solution for this, I'd personally use serialize and unserialize, the data stored under the 'ARRAY_STRINGS' part of your database can be serialized, when you unserialize them you'd have an array you can use in PHP without having to write a huge amount of code.

PHP:
1. array(MyItem,MyItem2);
2. array(MyKey=>MyItem,MyKey2=>MyItem2);
3. array(MyKey=>MyItem,MyKey2=>MyItem2,MyITem3=>MyITem3);
4. array(MyKey=>MyItem);
5. array(MyItem);

When using the above arrays as only the 'ARRAY_STRINGS' part this method would work with very minimal effort.

Yeah, but the actual array was generated from strings witch were then converted to php arrays, before that there was no php array representation of the array strings, since i am inputting array strings manually in the database...

thanks for response :)
 
http://www.php.net/manual/en/function.serialize.php
Generates a storable representation of a value.
This is useful for storing or passing PHP values around without losing their type and structure.


http://www.php.net/manual/de/function.unserialize.php
unserialize() takes a single serialized variable and converts it back into a PHP value.

serialize(mixed $value) serializes a data structure into a string representation that's unique to PHP and can be reversed into a PHP object using unserialize(string $str).

Example of serializing:
PHP:
$arr = array('key1' => 'value1', 'key2' => 'value2');
$serializedObj = serialize($arr); // returns a string representation. store it where you want

Example of unserializing:
PHP:
$strFromDatabase = "stored value";
$arr = unserialize($strFromDatabase);
echo $arr["key1"];


That's the cleanest solution for you.


// Edit: Sorry, haven't read the first reply. But.. nevermind.
 
Back