468,253 Members | 1,296 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,253 developers. It's quick & easy.

Splitting a string into arrays within an array but only one delimiter?

I'm trying to split a string into an separate arrays but the data is
only delimited by a comma. The actual data is one long string but the
info is in a regular format and repeats after every five ~'s .
i.e the data may be returned as
1.01~11844.69~0.0~0.0~2.0~1.02~117.3~0.0~0.0~0.0~1 .03~66.82~0.0~0.0~0.0~1.0*
4~300.0~0.0~0.0~0.0~
I guess I just have to loop through every five ~'s but unsure of the
best way using php , with C# I guess I could use length but haven't
too much of a clue using php. At the moment I'm even considering a
dirty fix using preg_replace to change the delimiter every 5 ~'s but
there must be a more correct way
Thanks
Jun 27 '08 #1
14 1534
sp***********@gmail.com wrote:
I'm trying to split a string into an separate arrays but the data is
only delimited by a comma. The actual data is one long string but the
info is in a regular format and repeats after every five ~'s .
i.e the data may be returned as
1.01~11844.69~0.0~0.0~2.0~1.02~117.3~0.0~0.0~0.0~1 .03~66.82~0.0~0.0~0.0~1.0*
4~300.0~0.0~0.0~0.0~
I guess I just have to loop through every five ~'s but unsure of the
best way using php , with C# I guess I could use length but haven't
too much of a clue using php. At the moment I'm even considering a
dirty fix using preg_replace to change the delimiter every 5 ~'s but
there must be a more correct way
Thanks
Can you show the source string, and the split results?
I don't understand what you meaning
Jun 27 '08 #2
On 10 Jun, 10:14, spreadbett...@gmail.com wrote:
I'm trying to split a string into an separate arrays but the data is
only delimited by a comma. The actual data is one long string but the
info is in a regular format and repeats after every five ~'s .

i.e the data may be returned as

1.01~11844.69~0.0~0.0~2.0~1.02~117.3~0.0~0.0~0.0~1 .03~66.82~0.0~0.0~0.0~1.0**
4~300.0~0.0~0.0~0.0~
You say: "the data is only delimited by a comma."
But the data you show us has no commas??

Please can you clarify this?
Jun 27 '08 #3
sp***********@gmail.com wrote:
I'm trying to split a string into an separate arrays but the data is
only delimited by a comma. The actual data is one long string but the
info is in a regular format and repeats after every five ~'s .
i.e the data may be returned as
1.01~11844.69~0.0~0.0~2.0~1.02~117.3~0.0~0.0~0.0~1 .03~66.82~0.0~0.0~0.0~1.0*
4~300.0~0.0~0.0~0.0~
I guess I just have to loop through every five ~'s but unsure of the
best way using php , with C# I guess I could use length but haven't
too much of a clue using php. At the moment I'm even considering a
dirty fix using preg_replace to change the delimiter every 5 ~'s but
there must be a more correct way
Thanks
Or explode() the string on '~' and take every 5 items and place them in
their own array.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 27 '08 #4
sp***********@gmail.com wrote:
I'm trying to split a string into an separate arrays but the data is
only delimited by a comma. The actual data is one long string but the
info is in a regular format and repeats after every five ~'s .
i.e the data may be returned as
1.01~11844.69~0.0~0.0~2.0~1.02~117.3~0.0~0.0~0.0~1 .03~66.82~0.0~0.0~0.0~1.0*
4~300.0~0.0~0.0~0.0~
I guess I just have to loop through every five ~'s but unsure of the
best way using php , with C# I guess I could use length but haven't
too much of a clue using php. At the moment I'm even considering a
dirty fix using preg_replace to change the delimiter every 5 ~'s but
there must be a more correct way
Thanks
If I understand you correctly, then:

$outer = explode('~',$theString);
for ($i=0; $i<count($first); $i++) {
$final[$i] = explode(',', $outer[$i]);
}

This gives a 2-D array for final.
Jun 27 '08 #5
Sorry about the confusion , was in a rush when posted it

The string data I want to split is in the format

1.01~11844.69~0.0~0.0~2.0~1.02~117.3~0.0~0.0~0.0~1 .03~66.82~0.0~0.0~0.0~1.0**
4~300.0~0.0~0.0~0.0~
Separated by tildes ~ not commas :(

Basically need to do what Jerry says explode it and take every 5 items
as a separate array , I'll take a look at sheldonig's example if I'm
assuming correctly tat $first would be 5 in my case ?

Thanks for al the posts and sorry again for the confusion with the
commas
Jun 27 '08 #6
On 10 Jun, 13:59, spreadbett...@gmail.com wrote:
Sorry about the confusion , was in a rush when posted it

The string data I want to split is in the format

1.01~11844.69~0.0~0.0~2.0~1.02~117.3~0.0~0.0~0.0~1 .03~66.82~0.0~0.0~0.0~1.0***
4~300.0~0.0~0.0~0.0~

Separated by tildes ~ not commas :(

Basically need to do what Jerry says explode it and take every 5 items
as a separate array , I'll take a look at sheldonig's example if I'm
assuming correctly tat $first would be 5 in my case ?

Thanks for al the posts and sorry again for the confusion with the
commas
sheldonlg's one won't work, because he was assuming that there were
some commas.

I would do an initial explode and then use array_slice()
http://uk.php.net/manual/en/function.array-slice.php to get my chunks
of 5.
Jun 27 '08 #7
Thanks Captain, I tried array_slice but it's does exactley what it
says on the tin and just slices off a part of the array, doesn't
actually put it anywhere useable, I'm sure I need to use count
somewhere along the line
Jun 27 '08 #8
Thanks again captain I was looking through the other array functions
on the php.net site and found exactly what I need in array_chunk.
Seems to do just what I needed
Jun 27 '08 #9
On 10 Jun, 15:24, spreadbett...@gmail.com wrote:
Thanks again captain I was looking through the other array functions
on the php.net site and found exactly what I need in array_chunk.
Seems to do just what I needed
array_slice() would have worked, but you are right, array_chunk() is
exactly what you need.
Jun 27 '08 #10
..oO(sp***********@gmail.com)
>Thanks Captain, I tried array_slice but it's does exactley what it
says on the tin and just slices off a part of the array, doesn't
actually put it anywhere useable
Have you actually read the manual entry? array_slice() returns the
extracted part.

Micha
Jun 27 '08 #11
On 10 Jun, 16:00, Michael Fesser <neti...@gmx.dewrote:
.oO(spreadbett...@gmail.com)
Thanks Captain, I tried array_slice but it's does exactley what it
says on the tin and just slices off a part of the array, doesn't
actually put it anywhere useable

Have you actually read the manual entry? array_slice() returns the
extracted part.

Micha
Yes I tried it but it only returned the slice, OK again not to clear
in my post due to trying to sort out my coding. I needed the whole of
the data being split into sections of 5 tildes not just a portion from
the end start or middle. I guess there's probably some way of using
slice but array chunk does it without the need for any added coding
Jun 27 '08 #12
..oO(sp***********@gmail.com)
>Yes I tried it but it only returned the slice, OK again not to clear
in my post due to trying to sort out my coding. I needed the whole of
the data being split into sections of 5 tildes not just a portion from
the end start or middle. I guess there's probably some way of using
slice
You would have to use a loop.
>but array chunk does it without the need for any added coding
Agreed. It's the better choice here.

Micha
Jun 27 '08 #13
sp***********@gmail.com wrote:
Sorry about the confusion , was in a rush when posted it

The string data I want to split is in the format

1.01~11844.69~0.0~0.0~2.0~1.02~117.3~0.0~0.0~0.0~1 .03~66.82~0.0~0.0~0.0~1.0**
4~300.0~0.0~0.0~0.0~
Separated by tildes ~ not commas :(

Basically need to do what Jerry says explode it and take every 5 items
as a separate array , I'll take a look at sheldonig's example if I'm
assuming correctly tat $first would be 5 in my case ?

Thanks for al the posts and sorry again for the confusion with the
commas
No. (and it is sheldonlg). I assumed that you had it comma separated
and that every five data items was separated from the next by a tilde --
because that is how I deciphered what you wrote. Ex:

1.01,11844.69,0.0,0.0,2.0~1.02,117.3,0.0,0.0,0.0~1 .03,66.82,0.0,0.0,0.0~

Since what is have is what you now show, it is:

$arr = explode('~', $theString); // 1-D array with all the data
$i = 0;
foreach ($arr as $x) {
$final[$i/5][$i%5] = $x;
$i++;
}

In this way you have $final as the 2-D array.
Jun 27 '08 #14
On 10 Jun, 17:34, sheldonlg <sheldonlgwrote:
spreadbett...@gmail.com wrote:
Sorry about the confusion , was in a rush when posted it
The string data I want to split is in the format
1.01~11844.69~0.0~0.0~2.0~1.02~117.3~0.0~0.0~0.0~1 .03~66.82~0.0~0.0~0.0~1.0***
4~300.0~0.0~0.0~0.0~
Separated by tildes ~ not commas :(
Basically need to do what Jerry says explode it and take every 5 items
as a separate array , I'll take a look at sheldonig's example if I'm
assuming correctly tat $first would be 5 in my case ?
Thanks for al the posts and sorry again for the confusion with the
commas

No. *(and it is sheldonlg). *I assumed that you had it comma separated
and that every five data items was separated from the next by a tilde --
because that is how I deciphered what you wrote. *Ex:

1.01,11844.69,0.0,0.0,2.0~1.02,117.3,0.0,0.0,0.0~1 .03,66.82,0.0,0.0,0.0~

Since what is have is what you now show, it is:

$arr = explode('~', $theString); *// 1-D array with all the data
$i = 0;
foreach ($arr as $x) {
* *$final[$i/5][$i%5] = $x;
* *$i++;

}

In this way you have $final as the 2-D array.- Hide quoted text -

- Show quoted text -
Thanks sheldonlg, my posting could have been made clearer :( Array
chunk gives me the same 2-D array and I guess is probably quicker with
less overheads but always good to learn something new as I'm sure I'll
be able to use your code on other things when combining arrays

$final=array_chunk(explode('~',$theString), 5);

Jun 27 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Adam Parkin | last post: by
5 posts views Thread by Ann Marinas | last post: by
1 post views Thread by Guadala Harry | last post: by
20 posts views Thread by Opettaja | last post: by
13 posts views Thread by Pedro Pinto | last post: by
2 posts views Thread by shadow_ | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.