1

I have, on a mysql table, a column in which are stored through a print_r on a previous step of the program.

So I have on a text cell of the mysql db this value:


Array
(
    [id] => 129
    [group_id] => 9
    [rid] => 28
    [date] => 2014-02-09 10:00:00
    [real_time_start] => 00:00:00
    [real_time_end] => 00:00:00
    [site_id] => 1
    [sub_site_id] => 2
    [team1_id] => 9
    [team2_id] => 13
    [home] => 2
    [result_type] => 2
    [status] => 0
)

I have tried several ways, but with no success. How can I transform this one in a "real" php array?

Do you have any idea about it?

Abdul Manaf
  • 4,768
  • 3
  • 27
  • 34
Gabriele
  • 57
  • 1
  • 1
  • 7
  • Instead of using `print_r` use some proper serialization like php's built in mechanism (`serialize/unserialize`) or json (`json_encode/json_decode`). However, more solid solution would be to normalize your database, i.e. create table with those columns and add relationship to existing table. For further information about relationships read up on: http://en.wikipedia.org/wiki/Relational_database Despite that php's manual has direct solution: http://www.php.net/manual/en/function.print-r.php#93529 – Leri Feb 18 '14 at 11:37
  • See http://www.php.net/manual/en/function.print-r.php#93529 – Anthony Sterling Feb 18 '14 at 11:46

4 Answers4

0

If you want to restore such data into readable PHP array do not use print_r() as its format is not easily readable by built-in parsers. You have several options:

  • convert it to JSON using json_encode() and then decode through json_decode,
  • convert to serialized format using var_export(), save to file and require() it,
  • use serialize() and unserialize(), as Leri said,
  • use YAML library (I recommend Symfony's one) and Yaml::dump() / Yaml::parse() your data.
Tomasz Kowalczyk
  • 10,472
  • 6
  • 52
  • 68
0

The print_r() function prints a variable in a human-readable format. Therefore, it has no reverse equivalent.

If you’re wanting to transform a representation of an array to a plain text version for storage and back again, then use something like PHP’s serialize() and unserialize() functions.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
0

To store an array to a table you should use serialize() function. When retrieving use unserialize() function to get the array back.

0

Thanks to all

The suggestion of Jasper was great, I've been able to convert the "print_r" output in a "serialize" one using the function proposed. Now I will use serialize/unserialize to solve this problem.

Gabriele
  • 57
  • 1
  • 1
  • 7