473,394 Members | 1,278 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,394 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 5126
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.