I have an array that is stored in the database, so the array must first be converted into a string. so how to return the array that I changed into the form of a string?
This is the array I have, with var_dump and xdebug:
barang.php:59:string '["warna" => ["merah","hitam","navy"], "ukuran" => ["40","41","42","43", "44"]]' (length=78)
See that it is a string, but the formatting inside represents an array.
I'd suggest cleaning up your database and saving the data in a better format. I'd suggest either serializing it with json_encode
or serialize
or normalizing the data into separate rows and writing custom code to read it out of the database.
But if you must work with this as is, you can get away with eval
here:
<?php
$string = '["warna" => ["merah","hitam","navy"], "ukuran" => ["40","41","42","43", "44"]]';
eval('$foo = ' . $string . ' ;');
var_dump($foo);
outputs:
array(2) {
["warna"]=>
array(3) {
[0]=>
string(5) "merah"
[1]=>
string(5) "hitam"
[2]=>
string(4) "navy"
}
["ukuran"]=>
array(5) {
[0]=>
string(2) "40"
[1]=>
string(2) "41"
[2]=>
string(2) "42"
[3]=>
string(2) "43"
[4]=>
string(2) "44"
}
}
Be mindful that using eval
is risky and be absolutely certain the data you're using it on is precisely what you think it is.