65

Looking for a way to produce a filename-safe hash of a given PHP array. I'm currently doing:

$filename = md5(print_r($someArray, true));

... but it feels "hacky" using print_r() to generate a string unique to each array.

Any bright ideas for a cleaner way to do this?

EDIT Well, seems everyone thinks serialize is better suited to the task. Any reason why? I'm not worried about ever retrieving information about the variable after it's hashed (which is good, since it's a one-way hash!). Thanks for the replies!

3
  • 2
    I think this is a pretty decent way of doing it, actually. Commented Feb 23, 2011 at 21:54
  • I would say it is equally effective both ways. However, considering the difference between them in their purpose, it seems more correct as a matter of principle to use serialize instead of print_r. But that's only if you have programmer OCD like me. :) Commented Sep 27, 2016 at 9:51
  • sha1 seems more unique. $sign = sha1(json_encode($data)); Commented Dec 4, 2020 at 20:24

5 Answers 5

111

Use md5(serialize()) instead of print_r().

print_r()'s purpose is primarily as a debugging function and is formatted for plain text display, whereas serialize() encodes an array or object representation as a compact text string for persistance in database or session storage (or any other persistance mechanism).

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

5 Comments

Thanks for the answer. Any reason serialize would be any better?
Would love to see some benchmarks for this method and it's alternatives.
@fieg: apparently serialize wins performance-wise. Here's the data: stackoverflow.com/a/32846231/680382
As pointed out on link metioned by @gogowitsch, you will need to also account for the ordering of the array.
This is the best one. I have extracted the necessary fields from the array to hash it better.
25

Alternatively you could use json_encode

1 Comment

Using json_encode to provide a hashing seed is not safe. For example, if the array you are using with json_encode has a non UTF-8 character, json_encode will return false, this will make all your md5 hashes the same.
14

serialize() should work fine.

It has the additional advantage of invoking the __sleep magic method on objects, and being the cleanest serialization method available in PHP overall.

1 Comment

I love how this is the first answer posted, and yet gotten right from the first go, without even one edit over all these years
10

What about serialize?

$filename = md5(serialize($someArray));

Comments

3

Using serialize() might be more conservative if you want to keep the type, etc...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.