array help

I am trying to write a function but am stopped at square one.
This snippet

<?php
error_reporting(E_ALL);
    $Buf = file("bsshort2.txt");
   print_r ($Buf);
$aKeys = array_keys($Buf);
print_r ($aKeys);
?>

prints out the file with all of the keys and then prints all of the keys as
expected.

This version

<?php
error_reporting(E_ALL);
    $Buf = file("bsshort2.txt");
   print_r ($Buf);
$aKeys = array_keys($Buf,("chuck"));
print_r ($aKeys);
?>

prints out the file with all of the keys and then prints Array ( )

This is part of the array that is printed

[9] => Beautiful night already 68, great sleeping weather.

[10] => chuck [11] =>

and this is from the file that is being read

I don’t want anybody dying on my watch PEROID. I just don’t like LTs.
hollering at me,GRIN


Beautiful night already 68, great sleeping weather.


chuck


I would expect to see

Array ( [10] => 10)

What I really want to search for is


but originally thought I was
having trouble searching for
so I tried “chuck”.

Where am I going wrong?

what are the contents of bsshort2.txt?

Copied from my post is the following:

[b]This is part of the array that is printed

[9] => Beautiful night already 68, great sleeping weather.

[10] => chuck [11] =>

and this is from the file that is being read

Beautiful night already 68, great sleeping weather.


chuck


[/b]

I didn’t put the whole file in my post in order to save space.
I just put in three of the keys (9, 10 & 11) and the text from the file that created them. I am assuming that key 11 is for


the key I really want to find.

Given what you have presented, I would expect to see data for 10 and 11 as they both contain the word “chuck”.

What confuses me more is why is 9 showing up when I don’t see the “chuck” in it?

Were you not expecting the “Values” of the particular array elements to show up as well?

I guess I have to learn how to express myself better.

The first code snippet

<?php
error_reporting(E_ALL);
    $Buf = file("bsshort2.txt");
   print_r ($Buf);
$aKeys = array_keys($Buf);
print_r ($aKeys);
?> [/code]

creates a web page with the following source 

[code]
Array
(
    [0] => <HR>

    [1] => <B>sandpile - </B>    Fri 31 Aug 2007 00:23:16 #0

    [2] => <P><B>stuff</B>

    [3] => <P>THOMAS P.--  Glad to hear your  DAD had a good trip through the  operating room.<P>

    [4] => We  had to  hustle a little  at the  prison today.  An inmate was dropping out with an insulin reaction.  There is a  rule in the TEXAS prison  system that an employee cannot give  an inmate anything in the way of medicene or treatment of an illness.  The  afore mentioned to be  handled by medical people.<BR>

    [5] => This same man  almost got me  fired---trying to die-- about a  year ago.  He was convulsing rapidly,(some times a ten minute wait before a  medic can reach you)  headed for an insulin caused coma.  We found  a tube of Glucose in his locker and give it to him.  WRONG  I  like to have lost the  core out of my ears,  from a LT.  saying  "DON'T  GIVE HIM THATTT"!!! <BR>

    [6] => The  inmates  sugar was 34 after another tube of glucose and a ten minute later test.  It was a lot closer than we knew.   Today I asked  his  cellie if  he  knew  how to give the glucose?  He says "I guess,  just squeeze a little at a time?  grin    When the LT. got there I was not giving the inmate  anything.GRIN<P>

    [7] => 

    [8] => I don't want anybody dying on my watch.PEROID  I just don't  like  LTs. hollering at  me,GRIN<P>

    [9] => Beautiful night already 68,  great sleeeping weather.<P>

    [10] => chuck

    [11] => <HR>

)
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
    [11] => 11
)
[/code]

The second snippet 

[code]<?php
error_reporting(E_ALL);
    $Buf = file("bsshort2.txt");
   print_r ($Buf);
$aKeys = array_keys($Buf,("chuck"));
print_r ($aKeys);
?> [/code]

creates a web page with the following source 

[code]Array
(
    [0] => <HR>

    [1] => <B>sandpile - </B>    Fri 31 Aug 2007 00:23:16 #0

    [2] => <P><B>stuff</B>

    [3] => <P>THOMAS P.--  Glad to hear your  DAD had a good trip through the  operating room.<P>

    [4] => We  had to  hustle a little  at the  prison today.  An inmate was dropping out with an insulin reaction.  There is a  rule in the TEXAS prison  system that an employee cannot give  an inmate anything in the way of medicene or treatment of an illness.  The  afore mentioned to be  handled by medical people.<BR>

    [5] => This same man  almost got me  fired---trying to die-- about a  year ago.  He was convulsing rapidly,(some times a ten minute wait before a  medic can reach you)  headed for an insulin caused coma.  We found  a tube of Glucose in his locker and give it to him.  WRONG  I  like to have lost the  core out of my ears,  from a LT.  saying  "DON'T  GIVE HIM THATTT"!!! <BR>

    [6] => The  inmates  sugar was 34 after another tube of glucose and a ten minute later test.  It was a lot closer than we knew.   Today I asked  his  cellie if  he  knew  how to give the glucose?  He says "I guess,  just squeeze a little at a time?  grin    When the LT. got there I was not giving the inmate  anything.GRIN<P>

    [7] => 

    [8] => I don't want anybody dying on my watch.PEROID  I just don't  like  LTs. hollering at  me,GRIN<P>

    [9] => Beautiful night already 68,  great sleeeping weather.<P>

    [10] => chuck

    [11] => <HR>

)
Array
(
)
 

I tried removing the () from around “chuck” and there was no change.
I would expect to see

Array
(
[10] => 10
)

not

Array
(
)

at the end of the second snippets output.

Darrell

Ok… I got you now…

However you will get a different result than what you expect.

Because of how you are populating the array (Each line to a key) you also grab the n (new line) at the end of each line. The search is looking for an EXACT match so if you modify your code as follows:

[php]

<?php error_reporting(E_ALL); $Buf = file("bsshort2.txt"); print_r ($Buf); $aKeys = array_keys($Buf,"chuckn"); // <------ Note the n here print_r ($aKeys); ?>

[/php]

Then you will get the result of

Array
(
    [0] => 10
)

Please note this “Array” shows the “Searched Results”

I tried adding the n to “chuck” and there was no change in output.

I have tried it and it works. I even added multiple “chuck” lines in the text file to see that it works.

Have a look at http://pgardner.net/test/ where the code and the output of both WITH and WITHOUT the search string.

When you add the n make sure it’s INSIDE the quotes.

Now I am getting really confused.
When I upload the snippet and text file to my web site it works like you say it does.
When I try to run it on http://localhost/test/short.php it runs like I posted.

I am running WampServer2.0a which has apache2.2.6 and php5.2.5 on my local machine.

My web server is running apache1.3.37 and php4.4.7

Is this the problem or am I running into a settings problem?

I suspect one is Windows and one is Linux. Windows and Linux do things a bit differently particularly with PATHS and Control. You might need to look for the rn instead on the WAMP machine. It depends on how the “TEXT” file is storing that.

In other words some use a CR and LF (Carrige Return and Line Feed) where others use just a “NewLine”

OK, that makes sense. I guess what I need to do to make it work in both cases is to strip rn from the array.
I tried using the following

error_reporting(E_ALL); $Buf = file("bsshort2.txt",FILE_IGNORE_NEW_LINES); print_r ($Buf); // $Buf = trim($Buf); $aKeys = array_keys($Buf,"<HR>r"); print_r ($aKeys);

but it only strips the n and leaves the r
I am not seeing the parameter or function that removes both.

I am not sure how helpful this would be but php.net has a listing that suggests using the rtrim() http://us3.php.net/manual/en/function.file.php#76367 to help resolve this, however, I am not sure at what point you would apply it in the context of what you are asking.

for example, to use the print_r you apply it to an array print_r($SomeArray) Thus there is no real opportunity to use the rtrim() function.

You could use it when you populate the array, however, because you use the file() function to populate the array, again there is no real option to apply the rtrim() function.

I suppose the Ultimate question, is what is your purpose for doing this. Since print_r() is generally a “Debugging” tool, if the goal is to search an array for a particular value and act accordingly on it (in a production setting), wouldn’t it be possible to use another tool to find the index/key such as array_search

You are right, I was using the snippet with print_r to debug.
What I am trying to do is find the keys for


so I can trim the text file leaving just the last 15
and the included text so as to shorten the file.
That way as people post the file stays basically the same size.

Thank you for all the help. I now have a function that keeps my text file
at a predetermined number of posts. Here is what I have

function shorten($filename,$len) { $Buf = file($filename); // Loads the text file into $Buf $aKeys = array_keys($Buf,"<HR>n"); / Finds all of the keys for the horizontal line rsort($aKeys); // Reverse sorts the keys $skey = $aKeys[$len]; // Stores the key requested $out = array_slice($Buf,$skey); // Stores the section of the array after the selected key to $out $save = implode("n",$out); // Converts the array to a string $myfile = fopen($filename,"w"); fwrite($myfile,$save); // Writes the shortened file fclose($myfile); }

and I call it with

shorten("bsshort.txt",50); // Shortens file to the last 51 posts shorten("bslong.txt",125); // Shortens file to the last 126 posts

after appending the new post to the file.

Sponsor our Newsletter | Privacy Policy | Terms of Service