-1

I've got a problem, if i retrieve a record from a database, it will return as a string. But I don't want that, because it should be an array:

var_dump($data)

returns

string(654) "Array ( [0] => Array ( [1] => 17-6-2015 [2] => Livingwater TEST [3] => J. Luttik [4] => E. Luttik [5] => Annemarie, Rosy [id] => 7 ) [1] => Array ( [1] => 21-6-2015 [2] => Celebration [3] => P. Brenner [4] => B. Nobbe [5] => Heleen, Laurens [id] => 8 ) [2] => Array ( [1] => 24-6-2015 [2] => Celebration [3] => C. Visser [4] => E. Luttik [5] => Annet, Elsemijn [id] => 9 ) ) "

How can i fix this?

Joel Hinz
  • 24,719
  • 6
  • 62
  • 75
Refilon
  • 3,334
  • 1
  • 27
  • 51
  • Why downvote? tell me what's wrong.... – Refilon Jun 05 '15 at 14:21
  • 1
    That is not a format that is easy or even possible to parse back into an array. Why is it in `var_dump` format to begin with?! – deceze Jun 05 '15 at 14:22
  • What are the outcome if you do `echo $data;`? – Cagatay Ulubay Jun 05 '15 at 14:24
  • Found this... http://stackoverflow.com/questions/3531857/convert-var-dump-of-array-back-to-array-variable – sanderbee Jun 05 '15 at 14:25
  • 2
    @sanderbee That'd be addressing the [wrong problem.](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – George Jun 05 '15 at 14:25
  • 1
    FWIW, I've voted to close this as *too broad*. While there may be an immediate answer to your immediate question, this is a symptom of something going quite wrong and it's unclear what exactly should be fixed in the broader scheme of things to bring whatever you're doing there to a professional level. – deceze Jun 05 '15 at 14:42

3 Answers3

4

I think I've got it, it's stored wrong into the database. It should be stored as json_encode($data), and retreived as json_decode($data, true).

But an other way is to store it as serialize($data) and retreive it with unserialize($data)

Refilon
  • 3,334
  • 1
  • 27
  • 51
  • 3
    Without analyzing the exact contents of the data, I would say this should be at least 2 tables each with multiple columns. If you normalize your database schema, you will be able to easily retrieve, sort, search for, etc. information. Serialized data, like json, in one field makes that next to impossible. – jeroen Jun 05 '15 at 14:37
2

I would use serialize to save the info in the db and unserialize to retrieve it to something useable. I believe it is the best way to do that. You can also use json_encode and json_decode, but I prefer [un]serialize

deceze
  • 510,633
  • 85
  • 743
  • 889
Miguel Mesquita Alfaiate
  • 2,851
  • 5
  • 30
  • 56
2

Ok, the clear fact is that the stored data is stored wrong! To reconstruct this string into an array, you have to do more than just storing it in a right way.

So the solution could be the one from @sanderbee: Convert var_dump of array back to array variable

Or my suggested solution would be storing them in a right way.

The data is an array so this means multible data. Either you save it as an serialized string, so you just can unserialize it or I think the better solution would be a 1-N relation inside the database, like:

Table: Humans

Rows:

  • ID
  • Name

Table: Pets

  • ID
  • Human_ID
  • Race

So 1 (ONE) Human can have N (0 til infinity) Pets.

Table: Human

  • 1, George Bush
  • 2, Barack Obama

Table: Pets

  • 1, 1, Dog
  • 2, 1, Cat
  • 3, 1, Delphin
  • 4, 2, Putin

So while George Bush has 3 Pets: A dog, a cat and a delphin. Barack Obama just have a putin as a pet.

This way you can easily get all data with one SQL-Query using JOIN statement.

$db->query( 'SELECT Human.Name, Pets.race FROM Human JOIN Pets ON ( Human.id = Pets.Human_ID ) ' );

Looks much more effort than unserialize? But much cleaner and if you got many data, the database will be much faster than.. or it should be.. also it's more cleaner. Looks more professionell for me.

Edit

Your profile looks like your experience should be enough to know this already.. is it possible that you "HAVE TO" use this records in the database? If so, than the very first option from sanderbee is a "must do"..

Community
  • 1
  • 1
Cagatay Ulubay
  • 2,491
  • 1
  • 14
  • 26
  • I've solved it by using a serialize() when storing it in the database, and unserialize when retrieving it. Works like a charm. – Refilon Jun 05 '15 at 14:51
  • Yea, yea, many ways leading you to your goal. But keep in mind, that some ways also makes problems follow your way and keep reaching you someday. If you have time you should try the database option too, it's really powerful and when you someday have tons of data to use, you will be thankfull having the experience. Not only it will help you to successfully beat the problem, but also increases your value as a developer for every company hiring :-) – Cagatay Ulubay Jun 05 '15 at 14:55
  • You're right, but the array wasn't from the database, but a google spreadsheet. So your story about the database is helpful, but not for the problem I had. It was stored as a print_r($value), so that was the problem. I've stored it as an serialize($value) now, and unserialize($value) when I want to get it out. Thanks anyways for your time. – Refilon Jul 03 '15 at 08:39