2

I have several associative arrays in PHP that looks like this:

$data1 = array("foo" => "one", "animal" => "mice");
$data2 = array("foo" => "two", "animal" => "cats");
....

I want to create another associative array, using the serialized values of the previous arrays are used as the array keys. For example:

$newArray = array("data1's serialized key" => "someNewValue", ... );

Are serialized arrays suitable for being used as array keys?

Do they contain any unacceptable characters?

Do I need to do something more to the serialized string to make it acceptable as an array key (while still keeping its uniqueness)?

1 Answer 1

1

Are serialized arrays suitable for being used as array keys?

Yup! As far as I know you can use serialized arrays as a key in another array. But the I cannot think of any use-case for this. :P

Do they contain any unacceptable characters?

No, until and unless you specify any unacceptable characters in the original array.

Do I need to do something more to the serialized string to make it acceptable as an array key (while still keeping its uniqueness)?

Nope.

So, you code would look like:

$data1 = array("foo" => "one", "animal" => "mice");
$data2 = array("foo" => "two", "animal" => "cats");
$serializedArrayKey1 = serialize($data1);
$serializedArrayKey2 = serialize($data2);
$newArray = array($serializedArrayKey1 => "Value for data1", ...);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.