?
Solved

PHP Converting print_r results that are stored in a database to a usable array again

Posted on 2009-04-08
3
Medium Priority
?
423 Views
Last Modified: 2012-05-06
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.

$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>";

Open in new window

0
Comment
Question by:tech1984
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 24103022
If you use var_export() instead of print_r(), it would be easier.

http://php.net/manual/en/function.var-export.php

This sort of thing is often done with serialize()/unserialize():

http://php.net/manual/en/function.serialize.php
http://php.net/manual/en/function.unserialize.php

0
 
LVL 3

Expert Comment

by:Duboux
ID: 24103187
Okay, first of all.. Adding an array-readout like that into a database is just uhhh... well.. u'd prolly be the first person that does this, lol

It seems like you want to have a database-table about your nightly packages per uhh.. maybe an accommodation :)

Why not have these tables:
  1. Table ACCOMODATION
    Columns:
    1. acc_id (int) (unique key) (auto increment)
    2. acc_name (varchar)
    3. acc_....
  2. Table NIGHT_PRICES
    1. np_id (int) (unique key) (auto increment)
    2. is_for_acc_id (int) <--- relates to ACCOMODATION.acc_id
    3. first_night (double) <--- price
    4. second_night (double) <--- price
    5. ....._night (double)  <--- price
ORRRRR

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:



$mysql_row = "title=2 Night Package|input_value=2 Night Package|price=;title=One Night Only|etc";
 
// Converting Text to Array
$TempArray = explode(";", $mysql_row);
$i = 0;
$MainArray = array();
foreach ($TempArray as $Row) {
    $i++;
    $Sub = explode("|", $Row);
    foreach ($Sub as $Detail) {
        $Detail = explode("=", $Detail);
        $Detail[1]  =  (isset($Detail[1]))  ?  $Detail[1]  :  "";  /// In case of no value this suppresses a "doesn't exist error...
        $MainArray[$i][$Detail[0]] = $Detail[1];
    }
}
print_r($MainArray);
 
 
 
// Converting Array to Text
$SqlArray = "";
$i = 0;
foreach ($MainArray as $Row) {  /// ;
    if ($i > 0) {  $SqlArray .= ";";  }
    $ii = 0;
    foreach ($Row as $key => $val) {  /// | and =
        if ($ii > 0) {  $SqlArray .= "|";  }
        $SqlArray .= $key."=".$val;
        $ii++;
    }
    $i++;
}
echo $SqlArray;

Open in new window

0
 

Author Closing Comment

by:tech1984
ID: 31568310
Awesome thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question