tech1984
asked on
PHP Converting print_r results that are stored in a database to a usable array again
I have a STING of text that looks like this, that is stored into a database:
Array ( [1] => Array ( [title] => 2 Night Package [input_value] => 2 Night Package [price] => ) [2] => Array ( [title] => One Night Only [input_value] => One Night Only [price] => -60.00 ) [3] => Array ( [title] => Add $60 for 3rd Night [input_value] => Add $60 for 3rd Night [price] => +60.00 ) [4] => Array ( [title] => Add $120 for 4th Night [input_value] => Add $120 for 4th Night [price] => +120.00 ) [5] => Array ( [title] => Add $180 for 5th Night [input_value] => Add $180 for 5th Night [price] => +180.00 ) )
the results of a print_r(array) is stored into a field. When I try to use it once echoed out onto a page, the php treats it like a string, righfully so I guess, however I need to great a select box using the above array. with the attached code. I almost wish there was something like to convert from string to array, like print_r can do this:
$look_i_am_a_string = print_r($array_here,true);
I really need something that would reverse this I guess.
Array ( [1] => Array ( [title] => 2 Night Package [input_value] => 2 Night Package [price] => ) [2] => Array ( [title] => One Night Only [input_value] => One Night Only [price] => -60.00 ) [3] => Array ( [title] => Add $60 for 3rd Night [input_value] => Add $60 for 3rd Night [price] => +60.00 ) [4] => Array ( [title] => Add $120 for 4th Night [input_value] => Add $120 for 4th Night [price] => +120.00 ) [5] => Array ( [title] => Add $180 for 5th Night [input_value] => Add $180 for 5th Night [price] => +180.00 ) )
the results of a print_r(array) is stored into a field. When I try to use it once echoed out onto a page, the php treats it like a string, righfully so I guess, however I need to great a select box using the above array. with the attached code. I almost wish there was something like to convert from string to array, like print_r can do this:
$look_i_am_a_string = print_r($array_here,true);
I really need something that would reverse this I guess.
$num_items = sizeof($myNewArray);
echo "<select id=\"s$i_opt\" onchange=\"build_array(this.value,'s$i_opt');\">";
for ($h=1;$h<$num_items;$h++)
{
$input_value = $myNewArray[$h]['input_value'];
$price = $myNewArray[$h]['price'];
$title = $myNewArray[$h]['title'];
if($price == '0.00')
{}else
{
$input_value = "$input_value|$price";
}
echo "<option value=\"$input_value\" title=\"$price\">";
echo "$title ($price)</option>";
}
echo "</select><br>";
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome thanks
It seems like you want to have a database-table about your nightly packages per uhh.. maybe an accommodation :)
Why not have these tables:
Columns:
instead of saving the print_r() (to which I'm curious on how you got to save that ;P)
have it saved like:
title=2 Night Package|input_value=2 Night Package|price=;title=One Night Only|etc...
You see I separate the values main arrays with a ;
and the sub-array details with a |
and the sub-array key and value with a =
Then you can read from and write to the table with:
Open in new window