PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > array
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
array

Réponse
 
LinkBack Outils de la discussion
Vieux 02/12/2007, 02h13   #1
Darrell
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut array

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<P>
Beautiful night already 68, great sleeping weather.<P>
chuck
<HR>

I would expect to see

Array ( [11] => 11)

What I really want to search for is <HR> but originally thought I was
having trouble searching for <HR> so I tried "chuck".



Where am I going wrong?






  Réponse avec citation
Vieux 02/12/2007, 23h31   #2
Darrell
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: array


"J.O. Aho" <user@example.net> wrote in message
news:5rj3liF14mecsU1@mid.individual.net...
> Darrell wrote:
>
>> <?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 ( )
>> Array ( [11] => 11)
>>
>> What I really want to search for is <HR> but originally thought I was
>> having trouble searching for <HR> so I tried "chuck".

>
> The search in the array_keys if for searching in the keys and not the
> content
> in the cells (there are plenty other functions for that).
>
> The parentheses around chuck don't give you any advantage, drop those and
> get
> a code that is easier to understand. Don't forget to use indentation and
> you
> make the code even easier to read.
>
> Not sure, but the following code may be more of use for you...
>
> $Buf = file("bsshort2.txt");
> foreach($Buf AS $key => $value) {
> echo $key." => ".$value."\n";
> if(!(stristr($value, '<HR>') === FALSE)) {
> echo "there was a horizontal line on the {$key} line\n";
> }
> }
>
> --
>
> //Aho

Thanks, That does get me closer and explains why I was getting no results
from

$aKeys = array_keys($Buf,("chuck"));
print_r ($aKeys);

I wrote that snippet just to see what the output was from array_keys because
nothing was working as expected.
What I was aiming for is an array with all of the keys for <HR> in a text
file
in order to trim the file to the last X number of occurrences of <HR>.
Darrell



  Réponse avec citation
Vieux 03/12/2007, 20h27   #3
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: array

Darrell wrote:

> <?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 ( )
> Array ( [11] => 11)
>
> What I really want to search for is <HR> but originally thought I was
> having trouble searching for <HR> so I tried "chuck".


The search in the array_keys if for searching in the keys and not the content
in the cells (there are plenty other functions for that).

The parentheses around chuck don't give you any advantage, drop those and get
a code that is easier to understand. Don't forget to use indentation and you
make the code even easier to read.

Not sure, but the following code may be more of use for you...

$Buf = file("bsshort2.txt");
foreach($Buf AS $key => $value) {
echo $key." => ".$value."\n";
if(!(stristr($value, '<HR>') === FALSE)) {
echo "there was a horizontal line on the {$key} line\n";
}
}

--

//Aho
  Réponse avec citation
Vieux 04/12/2007, 06h34   #4
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: array

Darrell wrote:

> I wrote that snippet just to see what the output was from array_keys because
> nothing was working as expected.
> What I was aiming for is an array with all of the keys for <HR> in a text
> file
> in order to trim the file to the last X number of occurrences of <HR>.
> Darrell


This should do the thing you thought about:
$Buf = file("bsshort2.txt");
foreach($Buf AS $key => $value) {
if(!(stristr($value, '<HR>') === FALSE)) {
$hrkeys[]=$key;
}
}


--

//Aho
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 09h20.


Édité par : vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09771 seconds with 12 queries