473,225 Members | 1,560 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,225 software developers and data experts.

How to convert array to string, and vice versa

I have some items, numbered from 0 upwards. Some of them may have a
string attached. All these items need to be represented in a single
already existing database record. So, I thought of taking an array, as
it might be looking thus (the values are all strings):

Key Value
--- -----
0 firstone
2 somestring
5 anotherstr
and so on, and converting it to a single string:

"'0', 'firstone', '2', 'somestring', '5', 'anotherstr'"

then I have a string I can write to the database record.

So I want to go from:

$myarr = array (0 => 'firstone', 2 => 'somestring', 5 => 'anotherstr');

to:

$mystring = "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'";

Is there a quick way to do this mapping in both directions? (I don't
mind if the string read/written to the database is not as above, as it
won't be used for any other purpose).

I can't see any array or string function that looks designed for this or
a similar purpose.

Thanks for any pointers.

-- tim
May 23 '06 #1
7 18672
Tim Streater wrote:
I have some items, numbered from 0 upwards. Some of them may have a
string attached. All these items need to be represented in a single
already existing database record. So, I thought of taking an array, as
it might be looking thus (the values are all strings):

Key Value
--- -----
0 firstone
2 somestring
5 anotherstr
and so on, and converting it to a single string:

"'0', 'firstone', '2', 'somestring', '5', 'anotherstr'"

then I have a string I can write to the database record.

So I want to go from:

$myarr = array (0 => 'firstone', 2 => 'somestring', 5 => 'anotherstr');

to:

$mystring = "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'";

Is there a quick way to do this mapping in both directions? (I don't
mind if the string read/written to the database is not as above, as it
won't be used for any other purpose).

I can't see any array or string function that looks designed for this or
a similar purpose.

Thanks for any pointers.

-- tim


Something like this could be used to pack the string:

$foreach( $myarr as $key => $value ) {
$tmp[] = "'$key'";
$tmp[] = "'$value'";
}
$mystring = '"'.implode(', ', $tmp).'"';

Unpacking would be something like:
$tmp = substr($mystring, 1, strlen($mystring)-1);
$myarr = explode(', ', $tmp);

You may have to fudge it a bit to get the quotes all correct.

-david-

May 23 '06 #2
Tim Streater said the following on 23/05/2006 17:49:
I have some items, numbered from 0 upwards. Some of them may have a
string attached. All these items need to be represented in a single
already existing database record. So, I thought of taking an array, as
it might be looking thus (the values are all strings):

Key Value
--- -----
0 firstone
2 somestring
5 anotherstr
and so on, and converting it to a single string:

"'0', 'firstone', '2', 'somestring', '5', 'anotherstr'"

then I have a string I can write to the database record.


Is it absolutely mandatory that you put these into a single record?
This is almost certainly a bad idea. Not only do you have to write
conversion functions to get data into and out of the database, but you
data is no longer atomic. Amongst other things, this makes it difficult
to search, index, delete or reference your data...
--
Oli
May 23 '06 #3
In article <xv****************@newsfe3-win.ntli.net>,
Oli Filth <ca***@olifilth.co.uk> wrote:
Tim Streater said the following on 23/05/2006 17:49:
I have some items, numbered from 0 upwards. Some of them may have a
string attached. All these items need to be represented in a single
already existing database record. So, I thought of taking an array, as
it might be looking thus (the values are all strings):

Key Value
--- -----
0 firstone
2 somestring
5 anotherstr
and so on, and converting it to a single string:

"'0', 'firstone', '2', 'somestring', '5', 'anotherstr'"

then I have a string I can write to the database record.


Is it absolutely mandatory that you put these into a single record?
This is almost certainly a bad idea. Not only do you have to write
conversion functions to get data into and out of the database, but you
data is no longer atomic. Amongst other things, this makes it difficult
to search, index, delete or reference your data...


The one record in question is for an interface card, typically in a
router or an SDH box. Up to now the ports on the card have been
considered identical and are numbered incrementally, so I have just kept
a record for the card, with a field numports. Now we wish to keep
information about the port optics (short-reach, long-reach, etc). With
the advent of plug-in optics this may vary from port to port.

The most general/flexible/futureproof way to do this would evidently be
to have a set of port-records, pointing to the interface card. Then,
obviously, the optics information would just be a field in the port
record.

I have resisted doing it this way up to now, because:

1) this would be a major change to the way the data is held, and more
importantly, displayed on our web pages, and interacted with.

2) it would prolly slow down the functionality a lot as there would be
an extra layer of things to do per port.

3) It's seemed a bit heavy to do this for just one bit of information
per port

4) I haven't got time to do this much work on this item.

So, I am looking for a quick'n'dirty fix :-) which will cause minimal
disruption on the rest of the structure.

I agree with your prospective downsides.

-- tim
May 23 '06 #4
In article <lf******************@fe55.usenetserver.com>,
David Haynes <da***********@sympatico.ca> wrote:
Tim Streater wrote:
I have some items, numbered from 0 upwards. Some of them may have a
string attached. All these items need to be represented in a single
already existing database record. So, I thought of taking an array, as
it might be looking thus (the values are all strings):

Key Value
--- -----
0 firstone
2 somestring
5 anotherstr
and so on, and converting it to a single string:

"'0', 'firstone', '2', 'somestring', '5', 'anotherstr'"

then I have a string I can write to the database record.

So I want to go from:

$myarr = array (0 => 'firstone', 2 => 'somestring', 5 => 'anotherstr');

to:

$mystring = "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'";

Is there a quick way to do this mapping in both directions? (I don't
mind if the string read/written to the database is not as above, as it
won't be used for any other purpose).

I can't see any array or string function that looks designed for this or
a similar purpose.

Thanks for any pointers.

-- tim


Something like this could be used to pack the string:

$foreach( $myarr as $key => $value ) {
$tmp[] = "'$key'";
$tmp[] = "'$value'";
}
$mystring = '"'.implode(', ', $tmp).'"';

Unpacking would be something like:
$tmp = substr($mystring, 1, strlen($mystring)-1);
$myarr = explode(', ', $tmp);

You may have to fudge it a bit to get the quotes all correct.


David,

No time to look at that carefully tonight but I'll check it tomorrow.

Thanks,

-- tim
May 23 '06 #5
tim

David Haynes wrote:
Tim Streater wrote:
So I want to go from:

$myarr = array (0 => 'firstone', 2 => 'somestring', 5 => 'anotherstr');

to:

$mystring = "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'";

Something like this could be used to pack the string:

$foreach( $myarr as $key => $value ) {
$tmp[] = "'$key'";
$tmp[] = "'$value'";
}
$mystring = '"'.implode(', ', $tmp).'"';

Unpacking would be something like:
$tmp = substr($mystring, 1, strlen($mystring)-1);
$myarr = explode(', ', $tmp);
array keys from the original $myarr are now array values in the new
$myarr
You may have to fudge it a bit to get the quotes all correct.
The quotes are a problem
-david-


What Oli said is right but if you want to do it this way then David's
suggestion of using a foreach/implode/explode would work.

You could make the packing and unpacking, especially the unpacking,
easier and cleaner if you do not quote the keys/values and if you use
two seperators. For example & and = from url query strings.

Tim

May 23 '06 #6
only read the title not your posting, but you might be searching for

serialize()
unserialize()

http://de.php.net/serialize
http://de.php.net/unserialize
May 23 '06 #7
In article <xv****************@newsfe3-win.ntli.net>,
Oli Filth <ca***@olifilth.co.uk> wrote:
Tim Streater said the following on 23/05/2006 17:49:
I have some items, numbered from 0 upwards. Some of them may have a
string attached. All these items need to be represented in a single
already existing database record. So, I thought of taking an array, as
it might be looking thus (the values are all strings):

Key Value
--- -----
0 firstone
2 somestring
5 anotherstr
and so on, and converting it to a single string:

"'0', 'firstone', '2', 'somestring', '5', 'anotherstr'"

then I have a string I can write to the database record.


Is it absolutely mandatory that you put these into a single record?
This is almost certainly a bad idea. Not only do you have to write
conversion functions to get data into and out of the database, but you
data is no longer atomic. Amongst other things, this makes it difficult
to search, index, delete or reference your data...


Oli,

Thanks for pushing me to re-think my strategy. Choosing a good data
representation is most of the battle and while driving home and back to
work this morning [1] I had a think about better ways to represent the
data which would retain the flexibility while not burdening the general
and presentation suite. Generally I don't like hacks anyway as they tend
to come back to bite you.

Thanks to the other responders - there are many functions in PHP I am
not familiar with so being pointed at some new ones for a specific
purpose will be a good exercise.

[1] Radio 4 only had an unfunny comedy program last night and a poor
magazine program this morning, as it happened, so I turned the radio off
while driving.

Cheers,

-- tim
May 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Byron | last post by:
Hello, I am a newbie and would like to know if it is possible to convert a string back to a dictionary? For example, I can convert a dictionary to a string by doing this: >>> names =...
5
by: Tim Wong | last post by:
All: I am trying to convert a CString value to an unsigned char array. I found some code online that will allow me to compile, but when I try to print out...i get a whole mess. /*Begin Code*/...
2
by: Steve - DND | last post by:
Just wondering if anyone out there has any code to convert a plural word to it's singular form and vice versa. Most of our database tables are named in a plural fashion. When we go to create...
1
by: War Eagle | last post by:
Is it possible to convert an unsigned long to byte array. For instance, unsigned long = 23480923 unsigned long longNumber; longNumber=23480923; // in binary = 1 0110 0110 0100 1010 0101 1011 //...
7
by: Matthias S. | last post by:
Hi, I had a look at the vast information on encryption in the MSDN and got pretty confused. All I want to do is to encrypt a string into an encrypted string and later decrypt that (encrypted)...
16
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as...
2
yabansu
by: yabansu | last post by:
Hi all, This is my first message! Using C++ standard libraries, I want to convert a string to a byte array and a byte array to the string. How can I do that? I did it in C# .NET as the...
6
yabansu
by: yabansu | last post by:
Hi all, I think most of you probably know the two .NET framework functions, namely Encoding.GetBytes(string) and Encoding.GetString(byte), to convert string into byte array and vice versa. Now,...
6
by: =?Utf-8?B?TFBldGVy?= | last post by:
Hi, I would copy the characters of a string variable to a fixed character buffer in a struct (and vice versa) in C#. public struct S { ... public fixed char cBuff; ...
6
by: Ole | last post by:
I have a class that I wish to convert into a byte array for sending over a serial line and vice versa - how do I do that? Thanks and regards Ole
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.