By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

Need help with exploding array

P: n/a
Here's my problem. I am reading from a text file using this:

if (!file_exists($file))
{
echo "Don't exist\n";
return false;
}

$fd = @fopen($file, 'r');
if (!is_resource($fd))
{
echo "Error reading\n";
return false;
}

And it works fine. The data that will be read will always be in the text
file in the form of

one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu

Now I want to explode that array that was read so I use this:

$arrayData = explode("-", fgets($fd, 4096));

print "first=$arrayData[0]\n";
print "second=$arrayData[1]\n";
print "third=$arrayData[2]\n";
print "fourth=$arrayData[3]\n";

which will give me

first=one second=two third=three fourth=alpha beta kappa delta epsilon
13.5pp 29.95eu

But I need to access the last two items (13.5pp and 29.95eu) of this
array to split them off as well so that the final output I need would be
like this:

first=one second=two third=three fourth=alpha beta kappa delta
epsilon.txt numpages=13.5pp cost=29.95eu

How can I do that? Do I need to further explode $arrayData[3] using a
space delimiter? Is there a way to grab the last two fields only because
the length of the 'alpha beta kappa delta epsilon' will not always be
the same but the numpages and cost will always be the last two elements
in the array. Any help would be appreciated.

Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Nathan Rose wrote:
Here's my problem. I am reading from a text file using this:

if (!file_exists($file))
{
echo "Don't exist\n";
return false;
}

$fd = @fopen($file, 'r');
if (!is_resource($fd))
{
echo "Error reading\n";
return false;
}

And it works fine. The data that will be read will always be in the text
file in the form of

one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu

Now I want to explode that array that was read so I use this:

$arrayData = explode("-", fgets($fd, 4096));

print "first=$arrayData[0]\n";
print "second=$arrayData[1]\n";
print "third=$arrayData[2]\n";
print "fourth=$arrayData[3]\n";

which will give me

first=one second=two third=three fourth=alpha beta kappa delta epsilon
13.5pp 29.95eu

But I need to access the last two items (13.5pp and 29.95eu) of this
array to split them off as well so that the final output I need would be
like this:

first=one second=two third=three fourth=alpha beta kappa delta
epsilon.txt numpages=13.5pp cost=29.95eu

How can I do that? Do I need to further explode $arrayData[3] using a
space delimiter?
Indeed.
Sounds logical.

Is there a way to grab the last two fields only because the length of the 'alpha beta kappa delta epsilon' will not always be
the same but the numpages and cost will always be the last two elements
in the array. Any help would be appreciated.


So use the last part of the first explode to explode again.
Check the number of elements you get back, take the last 2.

Regards,
Erwin Moller

Jul 17 '05 #2

P: n/a
Erwin Moller wrote:
Nathan Rose wrote:

Here's my problem. I am reading from a text file using this:

if (!file_exists($file))
{
echo "Don't exist\n";
return false;
}

$fd = @fopen($file, 'r');
if (!is_resource($fd))
{
echo "Error reading\n";
return false;
}

And it works fine. The data that will be read will always be in the text
file in the form of

one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu

Now I want to explode that array that was read so I use this:

$arrayData = explode("-", fgets($fd, 4096));

print "first=$arrayData[0]\n";
print "second=$arrayData[1]\n";
print "third=$arrayData[2]\n";
print "fourth=$arrayData[3]\n";

which will give me

first=one second=two third=three fourth=alpha beta kappa delta epsilon
13.5pp 29.95eu

But I need to access the last two items (13.5pp and 29.95eu) of this
array to split them off as well so that the final output I need would be
like this:

first=one second=two third=three fourth=alpha beta kappa delta
epsilon.txt numpages=13.5pp cost=29.95eu

How can I do that? Do I need to further explode $arrayData[3] using a
space delimiter?

Indeed.
Sounds logical.

Is there a way to grab the last two fields only because
the length of the 'alpha beta kappa delta epsilon' will not always be
the same but the numpages and cost will always be the last two elements
in the array. Any help would be appreciated.

So use the last part of the first explode to explode again.
Check the number of elements you get back, take the last 2.

Well at least it seems I am on the right track. :)

I had thought I remembered at one time seeing a solution about reversing
an array so that the last element was first, second to last was second,
etc. It just seems to be easier to explode the $arrayData[3], then
reverse it (into say $revArrayData) and take the reversed order as
$revArrayData[1] and $revArrayData[2].

Was I hallucinating about this or does something like it really exist?

Jul 17 '05 #3

P: n/a
Nathan Rose wrote:
Erwin Moller wrote:
Nathan Rose wrote:

Here's my problem. I am reading from a text file using this:

if (!file_exists($file))
{
echo "Don't exist\n";
return false;
}

$fd = @fopen($file, 'r');
if (!is_resource($fd))
{
echo "Error reading\n";
return false;
}

And it works fine. The data that will be read will always be in the text
file in the form of

one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu

Now I want to explode that array that was read so I use this:

$arrayData = explode("-", fgets($fd, 4096));

print "first=$arrayData[0]\n";
print "second=$arrayData[1]\n";
print "third=$arrayData[2]\n";
print "fourth=$arrayData[3]\n";

which will give me

first=one second=two third=three fourth=alpha beta kappa delta epsilon
13.5pp 29.95eu

But I need to access the last two items (13.5pp and 29.95eu) of this
array to split them off as well so that the final output I need would be
like this:

first=one second=two third=three fourth=alpha beta kappa delta
epsilon.txt numpages=13.5pp cost=29.95eu

How can I do that? Do I need to further explode $arrayData[3] using a
space delimiter?


Indeed.
Sounds logical.

Is there a way to grab the last two fields only because
the length of the 'alpha beta kappa delta epsilon' will not always be
the same but the numpages and cost will always be the last two elements
in the array. Any help would be appreciated.


So use the last part of the first explode to explode again.
Check the number of elements you get back, take the last 2.

Well at least it seems I am on the right track. :)

I had thought I remembered at one time seeing a solution about reversing
an array so that the last element was first, second to last was second,
etc. It just seems to be easier to explode the $arrayData[3], then
reverse it (into say $revArrayData) and take the reversed order as
$revArrayData[1] and $revArrayData[2].

Was I hallucinating about this or does something like it really exist?


Here: http://www.php.net/ bookmark it, use it. on the upper right there are
two boxes: search for[ ] in the [ ][v]

Enter: "reverse array" in the first box and select "functions list" or "whole
site" in the second -- see what you find... Again. Bookmark it. Use it. you
will be amazed at what you find...

--
Michael Austin.
Jul 17 '05 #4

P: n/a
Michael Austin wrote:
Nathan Rose wrote:
Here's my problem. I am reading from a text file using this:

if (!file_exists($file))
{
echo "Don't exist\n";
return false;
}

$fd = @fopen($file, 'r');
if (!is_resource($fd))
{
echo "Error reading\n";
return false;
}

And it works fine. The data that will be read will always be in the
text
file in the form of

one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu

Now I want to explode that array that was read so I use this:

$arrayData = explode("-", fgets($fd, 4096));

print "first=$arrayData[0]\n";
print "second=$arrayData[1]\n";
print "third=$arrayData[2]\n";
print "fourth=$arrayData[3]\n";

which will give me

first=one second=two third=three fourth=alpha beta kappa delta epsilon
13.5pp 29.95eu

But I need to access the last two items (13.5pp and 29.95eu) of this
array to split them off as well so that the final output I need
would be
like this:

first=one second=two third=three fourth=alpha beta kappa delta
epsilon.txt numpages=13.5pp cost=29.95eu

the length of the 'alpha beta kappa delta epsilon' will not always be
the same but the numpages and cost will always be the last two elements
in the array. Any help would be appreciated.


I had thought I remembered at one time seeing a solution about
reversing an array so that the last element was first, second to last
was second, etc. It just seems to be easier to explode the
$arrayData[3], then reverse it (into say $revArrayData) and take the
reversed order as $revArrayData[1] and $revArrayData[2].

Was I hallucinating about this or does something like it really exist?


Here: http://www.php.net/ bookmark it, use it. on the upper right
there are two boxes: search for[ ] in the [ ][v]

Enter: "reverse array" in the first box and select "functions list" or
"whole site" in the second -- see what you find... Again. Bookmark it.
Use it. you will be amazed at what you find...


Thank you. I have gotten the reserse_array to work as I need. But I have
one more question that I can't find an answer to by searching.

How can I print the $arrayData[3] and leave off the last two items
(13.5pp 29.95eu) so that the array would print as:
fourth=alpha beta kappa delta epsilon
instead of
fourth=alpha beta kappa delta epsilon.txt 13.5pp 29.95eu

Jul 17 '05 #5

P: n/a
Nathan Rose wrote:
Michael Austin wrote:
Nathan Rose wrote:
> Here's my problem. I am reading from a text file using this:
>
> if (!file_exists($file))
> {
> echo "Don't exist\n";
> return false;
> }
>
> $fd = @fopen($file, 'r');
> if (!is_resource($fd))
> {
> echo "Error reading\n";
> return false;
> }
>
> And it works fine. The data that will be read will always be in the
> text
> file in the form of
>
> one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu
>
> Now I want to explode that array that was read so I use this:
>
> $arrayData = explode("-", fgets($fd, 4096));
>
> print "first=$arrayData[0]\n";
> print "second=$arrayData[1]\n";
> print "third=$arrayData[2]\n";
> print "fourth=$arrayData[3]\n";
>
> which will give me
>
> first=one second=two third=three fourth=alpha beta kappa delta epsilon
> 13.5pp 29.95eu
>
> But I need to access the last two items (13.5pp and 29.95eu) of this
> array to split them off as well so that the final output I need
> would be
> like this:
>
> first=one second=two third=three fourth=alpha beta kappa delta
> epsilon.txt numpages=13.5pp cost=29.95eu
>
> the length of the 'alpha beta kappa delta epsilon' will not always be
> the same but the numpages and cost will always be the last two
> elements in the array. Any help would be appreciated.


I had thought I remembered at one time seeing a solution about
reversing an array so that the last element was first, second to last
was second, etc. It just seems to be easier to explode the
$arrayData[3], then reverse it (into say $revArrayData) and take the
reversed order as $revArrayData[1] and $revArrayData[2].

Was I hallucinating about this or does something like it really exist?


Here: http://www.php.net/ bookmark it, use it. on the upper right
there are two boxes: search for[ ] in the [ ][v]

Enter: "reverse array" in the first box and select "functions list" or
"whole site" in the second -- see what you find... Again. Bookmark it.
Use it. you will be amazed at what you find...


Thank you. I have gotten the reserse_array to work as I need. But I have
one more question that I can't find an answer to by searching.

How can I print the $arrayData[3] and leave off the last two items
(13.5pp 29.95eu) so that the array would print as:
fourth=alpha beta kappa delta epsilon
instead of
fourth=alpha beta kappa delta epsilon.txt 13.5pp 29.95eu

Use count($somearray) to get back the number of elements.
example:
$arr[0]="hai";
$arr[1]="hello";
$arr[3]="hoe do ya do?";

count($arr) will return 3.

so... you should be able to figure it our yourself how to strip the last 2
elements after your explode.

Regards,
Erwin Moller

Jul 17 '05 #6

P: n/a
Erwin Moller wrote:
Nathan Rose wrote:

Michael Austin wrote:

>Nathan Rose wrote:
>
>
>
>>Here's my problem. I am reading from a text file using this:
>>
>> if (!file_exists($file))
>> {
>> echo "Don't exist\n";
>> return false;
>> }
>>
>> $fd = @fopen($file, 'r');
>> if (!is_resource($fd))
>> {
>> echo "Error reading\n";
>> return false;
>> }
>>
>>And it works fine. The data that will be read will always be in the
>>text
>>file in the form of
>>
>>one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu
>>
>>Now I want to explode that array that was read so I use this:
>>
>>$arrayData = explode("-", fgets($fd, 4096));
>>
>>print "first=$arrayData[0]\n";
>>print "second=$arrayData[1]\n";
>>print "third=$arrayData[2]\n";
>>print "fourth=$arrayData[3]\n";
>>
>>which will give me
>>
>>first=one second=two third=three fourth=alpha beta kappa delta epsilon
>>13.5pp 29.95eu
>>
>>But I need to access the last two items (13.5pp and 29.95eu) of this
>>array to split them off as well so that the final output I need
>>would be
>>like this:
>>
>>first=one second=two third=three fourth=alpha beta kappa delta
>>epsilon.txt numpages=13.5pp cost=29.95eu
>>
>
>
>>the length of the 'alpha beta kappa delta epsilon' will not always be
>>the same but the numpages and cost will always be the last two
>>elements in the array. Any help would be appreciated.
>
I had thought I remembered at one time seeing a solution about
reversing an array so that the last element was first, second to last
was second, etc. It just seems to be easier to explode the
$arrayData[3], then reverse it (into say $revArrayData) and take the
reversed order as $revArrayData[1] and $revArrayData[2].

Was I hallucinating about this or does something like it really exist?
Here: http://www.php.net/ bookmark it, use it. on the upper right
there are two boxes: search for[ ] in the [ ][v]

Enter: "reverse array" in the first box and select "functions list" or
"whole site" in the second -- see what you find... Again. Bookmark it.
Use it. you will be amazed at what you find...


Thank you. I have gotten the reserse_array to work as I need. But I have
one more question that I can't find an answer to by searching.

How can I print the $arrayData[3] and leave off the last two items
(13.5pp 29.95eu) so that the array would print as:
fourth=alpha beta kappa delta epsilon
instead of
fourth=alpha beta kappa delta epsilon.txt 13.5pp 29.95eu


Use count($somearray) to get back the number of elements.
example:
$arr[0]="hai";
$arr[1]="hello";
$arr[3]="hoe do ya do?";

count($arr) will return 3.

so... you should be able to figure it our yourself how to strip the last 2
elements after your explode.


Thank you. I understand about finding out the count of the array and if
every line that was being read into the array was the same length, I
could figure out how to leave of the last two elements easily. But as I
stated earlier, the length of the 'alpha beta kappa delta epsilon' will
not always be the same but the numpages and cost will always be the last
two elements in the array.

So I'm back to the question of how can I print the $arrayData[3] data
and leave off the last two elements when the length of the $arrayData[3]
data will vary from line to line?

Jul 17 '05 #7

P: n/a
Nathan Rose wrote:
Erwin Moller wrote:
Nathan Rose wrote:

Michael Austin wrote:
>>Nathan Rose wrote:
>>
>>
>>
>>>Here's my problem. I am reading from a text file using this:
>>>
>>> if (!file_exists($file))
>>> {
>>> echo "Don't exist\n";
>>> return false;
>>> }
>>>
>>> $fd = @fopen($file, 'r');
>>> if (!is_resource($fd))
>>> {
>>> echo "Error reading\n";
>>> return false;
>>> }
>>>
>>>And it works fine. The data that will be read will always be in the
>>>text
>>>file in the form of
>>>
>>>one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu
>>>
>>>Now I want to explode that array that was read so I use this:
>>>
>>>$arrayData = explode("-", fgets($fd, 4096));
>>>
>>>print "first=$arrayData[0]\n";
>>>print "second=$arrayData[1]\n";
>>>print "third=$arrayData[2]\n";
>>>print "fourth=$arrayData[3]\n";
>>>
>>>which will give me
>>>
>>>first=one second=two third=three fourth=alpha beta kappa delta
>>>epsilon 13.5pp 29.95eu
>>>
>>>But I need to access the last two items (13.5pp and 29.95eu) of this
>>>array to split them off as well so that the final output I need
>>>would be
>>>like this:
>>>
>>>first=one second=two third=three fourth=alpha beta kappa delta
>>>epsilon.txt numpages=13.5pp cost=29.95eu
>>>
>>
>>
>>>the length of the 'alpha beta kappa delta epsilon' will not always be
>>>the same but the numpages and cost will always be the last two
>>>elements in the array. Any help would be appreciated.
>>
>
>
>I had thought I remembered at one time seeing a solution about
>reversing an array so that the last element was first, second to last
>was second, etc. It just seems to be easier to explode the
>$arrayData[3], then reverse it (into say $revArrayData) and take the
>reversed order as $revArrayData[1] and $revArrayData[2].
>
>Was I hallucinating about this or does something like it really exist?
>

Here: http://www.php.net/ bookmark it, use it. on the upper right
there are two boxes: search for[ ] in the [ ][v]

Enter: "reverse array" in the first box and select "functions list" or
"whole site" in the second -- see what you find... Again. Bookmark it.
Use it. you will be amazed at what you find...
Thank you. I have gotten the reserse_array to work as I need. But I have
one more question that I can't find an answer to by searching.

How can I print the $arrayData[3] and leave off the last two items
(13.5pp 29.95eu) so that the array would print as:
fourth=alpha beta kappa delta epsilon
instead of
fourth=alpha beta kappa delta epsilon.txt 13.5pp 29.95eu


Use count($somearray) to get back the number of elements.
example:
$arr[0]="hai";
$arr[1]="hello";
$arr[3]="hoe do ya do?";

count($arr) will return 3.

so... you should be able to figure it our yourself how to strip the last
2 elements after your explode.


Thank you. I understand about finding out the count of the array and if
every line that was being read into the array was the same length, I
could figure out how to leave of the last two elements easily. But as I
stated earlier, the length of the 'alpha beta kappa delta epsilon' will
not always be the same but the numpages and cost will always be the last
two elements in the array.

So I'm back to the question of how can I print the $arrayData[3] data
and leave off the last two elements when the length of the $arrayData[3]
data will vary from line to line?


Hi Nathan,

I think you misunderstand count.
Or maybe I am misunderstanding you. Could very well be at a fridayafternoon.
:-)

Count does not return the number of character, but the number of elements in
an array.

So if you do not know what is happening before 13.5pp 29.95eu, you can still
take them off.

alpha beta kappa delta epsilon 13.5pp 29.95eu
or
alpha beta 13.5pp 29.95eu

could both be treated this way.
So what you need to do is this:
1) explode
2) count the number of elements
3) use only the first (total elements -2) to return.

Here is an example.

<?
$test1 = "test gamma beta 13.5pp 29.95eu";
$test2 = "alpha beta kappa delta epsilon 13.5pp 29.95eu";

echo "stripLast2(".$test1.")= ".stripLast2($test1)."<br>";
echo "stripLast2(".$test2.")= ".stripLast2($test2)."<br>";

function stripLast2($someString){
$elements=explode(" ",$someString);
$returnString = "";
for ($i=0;$i<count($elements)-2;$i++){$returnString.=$elements[$i]." "; }
return $returnString;
}
?>
Regards,
Erwin Moller
Jul 17 '05 #8

P: n/a
"Nathan Rose" <nr***@spam.com> wrote in message
news:Mo*****************@newsread3.news.atl.earthl ink.net...
Here's my problem. I am reading from a text file using this:

if (!file_exists($file))
{
echo "Don't exist\n";
return false;
}

$fd = @fopen($file, 'r');
if (!is_resource($fd))
{
echo "Error reading\n";
return false;
}

And it works fine. The data that will be read will always be in the text
file in the form of

one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu

Now I want to explode that array that was read so I use this:

$arrayData = explode("-", fgets($fd, 4096));

print "first=$arrayData[0]\n";
print "second=$arrayData[1]\n";
print "third=$arrayData[2]\n";
print "fourth=$arrayData[3]\n";

which will give me

first=one second=two third=three fourth=alpha beta kappa delta epsilon
13.5pp 29.95eu

But I need to access the last two items (13.5pp and 29.95eu) of this
array to split them off as well so that the final output I need would be
like this:

first=one second=two third=three fourth=alpha beta kappa delta
epsilon.txt numpages=13.5pp cost=29.95eu

How can I do that? Do I need to further explode $arrayData[3] using a
space delimiter? Is there a way to grab the last two fields only because
the length of the 'alpha beta kappa delta epsilon' will not always be
the same but the numpages and cost will always be the last two elements
in the array. Any help would be appreciated.


This kind of stuff is easy as slicing cheese with regular expression.
Without regexp it's rather painful.

$s = "one - two - three - alpha beta kappa delta epsilon 13.5pp
29.95eu\r\n";
if(preg_match('/
\s*(.*?)\s*-
\s*(.*?)\s*-
\s*(.*?)\s*-
\s*(.*?)\s+
([\d\.]+)\s*pp\s+
([\d\.]+)\s*eu\s*
/xs', $s, $matches)) {
$one = $matches[1];
$two = $matches[2];
$three = $matches[3];
$four = $matches[4];
$numpages= $matches[5];
$cost= $matches[6];
print_r(compact('one', 'two', 'three', 'four', 'numpages', 'cost'));
}
Jul 17 '05 #9

P: n/a
Erwin Moller wrote:

Count does not return the number of character, but the number of elements in
an array.

So if you do not know what is happening before 13.5pp 29.95eu, you can still
take them off.

alpha beta kappa delta epsilon 13.5pp 29.95eu
or
alpha beta 13.5pp 29.95eu

could both be treated this way.
So what you need to do is this:
1) explode
2) count the number of elements
3) use only the first (total elements -2) to return.

Here is an example.

<?
$test1 = "test gamma beta 13.5pp 29.95eu";
$test2 = "alpha beta kappa delta epsilon 13.5pp 29.95eu";

echo "stripLast2(".$test1.")= ".stripLast2($test1)."<br>";
echo "stripLast2(".$test2.")= ".stripLast2($test2)."<br>";

function stripLast2($someString){
$elements=explode(" ",$someString);
$returnString = "";
for ($i=0;$i<count($elements)-2;$i++){$returnString.=$elements[$i]." "; }
return $returnString;
}
?>


Sorry for not getting back to you sooner Erwin. I played around with
this example last night and got it working for my purposes. It's
throwing a few warnings on me but nothing I can't live with. This is an
internal script only. It will not be up on the web so I'll add a couple
of @ to suppress.

I didn't grasp the ($i=0;$i<count($elements)-2;$i++) concept. That's why
I couldn't figure out how to drop only the last two.

Thank you for your assistance. I appreciate the effort you put forth to
help me.

Nathan

Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.