I have serialized array. It is serialized into string looking like:
*field1_name*|*field1_value*;*field2_name*|*field2_value*;*field3_name*|*field3_value*
etc.
I need to get value of field 3.
At this moment i get it by:
explode("*|*", explode("*;*", serialized_array)[3])[1]
but i’m almost sure that there is better way to do this.
Is in php function that allows to turn it into associative array?
3
Answers
If i understand Your idea step 1 should looks like:
All of "*", "|", ";" can be in field_value, so replace all of them isn't a good way.
This approach is more flexible than using multiple
explode()
calls and will work for an arbitrary number of fields in your serialized string.Very-very-very basic solution:
In the real world, errors may need to be handled