-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?

6
  • Why downvote? tell me what's wrong.... Commented Jun 5, 2015 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?! Commented Jun 5, 2015 at 14:22
  • What are the outcome if you do echo $data;? Commented Jun 5, 2015 at 14:24
  • 2
    @sanderbee That'd be addressing the wrong problem. Commented Jun 5, 2015 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. Commented Jun 5, 2015 at 14:42

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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"..

3 Comments

I've solved it by using a serialize() when storing it in the database, and unserialize when retrieving it. Works like a charm.
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 :-)
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.
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

7 Comments

Meeeeehhh..... so close... What is "that" exactly? This advice is not wrong, but also far from right.
the text was not as explicit as I would wanted :)
OK, better. Though I can't agree with storing serialized data in a database.
@deceze any specific reason why?
This question is a train wreck in slow motion. Be glad you can't see all the deleted answers. Yours wasn't good enough to survive the immediate downvote convulsion going on here.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.