472,142 Members | 1,037 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 software developers and data experts.

Unserialize a single array element in a constant?

Hello all,

I have a question about unserializing a single array element from a
serialized array. Can this be done, or must I first unserialize the
array, and then access the element?

For example, given:

$data = serialize(array('4.50','0.00','0.00'));

Can I unserialize individual elements from $data, such as:

$data_element = unserialize($data[2]);

As it is now, PHP will generate a syntax error, forcing me instead to
unserialize the entire array before accessing it:

$new_data = unserialize($data);
$data_element = $new_data[2];

NOTE the value in this is as it relates to CONSTANTS, where arrays must
be serialized before define()'d as a constant.

thanks,

rich

Jul 17 '05 #1
7 5010
ri****@gmail.com wrote:
I have a question about unserializing a single array element from a
serialized array. Can this be done, or must I first unserialize the
array, and then access the element?
You either need to unserialize the array first, or write your own
unserializer which skips the parts of the string you're not interested in.
Can I unserialize individual elements from $data, such as:

$data_element = unserialize($data[2]);
$data is a string. $data[2] is the third byte in the string. For obvious
reasons, you can't unserialize much of anything out of that single byte.
NOTE the value in this is as it relates to CONSTANTS, where arrays must
be serialized before define()'d as a constant.


Is there any particular reason you're doing that?

-- brion vibber (brion @ pobox.com)
Jul 17 '05 #2
Thanks for the response Brion.
From reading the documentation on php.net for the define() function,

there is a comment made in User Contributed Notes which states:

22-Sep-2003 06:13
Constants MUST evaluate to scalar values only.
You are encouraged to use serialize/unserlialize
to store/retrieve an array in a constant:

define('CONST',serialize(array('a','b','foo'=>'bar ')));
var_dump(CONST);
var_dump(unserialize(CONST));

While clearly, any user contribution should be taken with a grain of
salt, are you suggesting that it is not necessary to first serialize an
array before making it a constant?

Let me generalize my original post to a single question:

How do I create a constant array?

And then, by extension, how do I access a single constant array
element?

I had presumed that an array must first be serialized. Perhaps that's a
bad assumption.

thanks,

rich

Jul 17 '05 #3
> How do I create a constant array?

You can't as of my knowledge, and neither can you access elements in the
array directly without either unserializing it or parsing the serialized
data.

Maybe you want to overlook your need of constant arrays?

Constants as you find in Java and C++ is simply not supported in PHP.

Regards,

Peter

<ri****@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Thanks for the response Brion.
From reading the documentation on php.net for the define() function,

there is a comment made in User Contributed Notes which states:

22-Sep-2003 06:13
Constants MUST evaluate to scalar values only.
You are encouraged to use serialize/unserlialize
to store/retrieve an array in a constant:

define('CONST',serialize(array('a','b','foo'=>'bar ')));
var_dump(CONST);
var_dump(unserialize(CONST));

While clearly, any user contribution should be taken with a grain of
salt, are you suggesting that it is not necessary to first serialize an
array before making it a constant?

Let me generalize my original post to a single question:

How do I create a constant array?

And then, by extension, how do I access a single constant array
element?

I had presumed that an array must first be serialized. Perhaps that's a
bad assumption.

thanks,

rich

Jul 17 '05 #4
ri****@gmail.com wrote:
22-Sep-2003 06:13
Constants MUST evaluate to scalar values only.
You are encouraged to use serialize/unserlialize
to store/retrieve an array in a constant: [snip] While clearly, any user contribution should be taken with a grain of
salt, are you suggesting that it is not necessary to first serialize an
array before making it a constant?
No, I'm wondering why you're trying to use a constant for an array, when
you already know that constants can't contain arrays. I'm wondering if
there's some specific reason that you MUST use a constant for this (in
which case a workaround such as serialization/unserialization or
implode/explode would be necessary to get an array value out of a
string) or if you're simply making your task more difficult by picking
an unsuitable tool without a good reason for it.
Let me generalize my original post to a single question:

How do I create a constant array?
You can't.

See: http://www.php.net/manual/en/language.constants.php
And then, by extension, how do I access a single constant array
element?


Constants cannot have array values, so there is no such thing as a
constant array element.

Use a variable or some other way of storing your data as an array if you
need to be able to access elements of an array value directly.

-- brion vibber (brion @ pobox.com)
Jul 17 '05 #5
Thanks Peter, that confirms my recent experience.

I can certainly get by using a standard array, but--as you guessed--I'm
coming from the C/C++ world, and was looking for an equivalent analog.

rich

Jul 17 '05 #6
Thanks for the clarification, Brion.

However, constants can contain arrays, BUT they must first be
serialized, and... where I had my particular question, it is the case
that they must then be unserialized first before accessing them. Though
I suppose, technically, if an array is first serialized before
define()'d, one could argue that what's actually contained in the
constant is a string (a scalar which CAN be stored as a constant).

In any case, it would be very hard to defend the act of creating a
constant array from any kind of performance metric: I'm quite certain
that the overhead of first serializing and then unserializing such a
structure is, while not prohibitive, expensive.

I have my answers.

thanks again,

rich

Jul 17 '05 #7
<ri****@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Thanks for the response Brion.
From reading the documentation on php.net for the define() function,

there is a comment made in User Contributed Notes which states:

22-Sep-2003 06:13
Constants MUST evaluate to scalar values only.
You are encouraged to use serialize/unserlialize
to store/retrieve an array in a constant:

define('CONST',serialize(array('a','b','foo'=>'bar ')));
var_dump(CONST);
var_dump(unserialize(CONST));

While clearly, any user contribution should be taken with a grain of
salt, are you suggesting that it is not necessary to first serialize an
array before making it a constant?


Hmmmm. A pretty lame way to get around a limitation. Do you absolutely have
to use a constant? Why not just define a function?
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mark | last post: by
2 posts views Thread by Andrew | last post: by
204 posts views Thread by Alexei A. Frounze | last post: by
5 posts views Thread by Mike | last post: by
15 posts views Thread by DL | last post: by
7 posts views Thread by John Koleszar | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.