473,322 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 5121
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark | last post by:
Greetings: So, I've got a real problem in that PHP doesn't seem to be able to unserialize() an array that has an element containing a newline. Of course, I've used htmlentities() to protect the...
2
by: Andrew | last post by:
Some have suggested that using serialize() and unserialize is faster than reading/writing an array to disk as a simple text file using $array = file('numbers.txt'); Can anyone justify this? ...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
5
by: Mike | last post by:
I finally got serialize to work but now I cannot get unserialize to work. Here is my code. $settings = serialize($settings_array); echo "<INPUT TYPE='Hidden' NAME='settings'...
7
by: =?utf-8?B?5YiY5piK?= | last post by:
Hi, folks, Is it possible to delete an element from a sorted array with O(1) time? Best regards
15
by: DL | last post by:
say, we have the following: // declare an array myArray = ; // short hand? same as myArray = new Array ? // populate it myArray = 0; myArray = 0; myArray = 1; myArray = 0;
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
7
by: Christof Warlich | last post by:
Hi, the subject says it all: I need to instantiate an array of objects where each object "knows" its arrary index. So far, this is easy as long as index is not a compile-time constant: class ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.