PHP Array Help - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP Array Help (/showthread.php?tid=89279)
PHP Array Help by stoshrocket on 02-19-2009 at 09:07 PM
Hey Guys,
I've just started up a little project after years of inactivity in the coding world, but hit a bit of a snag. Simplified and to just deal with the root of my problem let's consider the following:
I have three files, index.php, one_list.txt and two_list.txt.
The contents of these files is as follows:
one_list.txt:
code: monkey
rhino
ironing_board
two_list.txt:
code: penguin
baguette
pillow
index.php:
code: <?php
$one = "monkey";
$two = "baguette";
$one_array = file("one_list.txt");
$two_array = file("two_list.txt");
if (in_array("$one",$one_array)){
echo"one present!<br />";
if(in_array("$two",$two_array)){
echo"two present!";
}
else{
echo"two missing!";
}
}
else{
echo"one missing!";
}
?>
Now, basically, when run the index page should set the two variables $one and $two, then set the arrays $one_array and $two_array using the two text files, then check $one_array for $one, and if successful check $two_array for $two, but this isn't working. When I run index.php $one is not found within the array, even though it clearly is in the text file... Can anyone see why? Thanks =)
RE: PHP Array Help by matty on 02-19-2009 at 09:14 PM
I didn't know the file function read the entire contents of the file to an array...
php code: <?php
$one = "monkey";
// the HTML source of a URL.
$lines = file('./one_list.txt');
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
if ($line == $one) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
}
?>
Or better yet why are you putting your variables in ""
php code: <?php
$one = "monkey";
$two = "baguette";
$one_array = file("one_list.txt");
$two_array = file("two_list.txt");
>>>if (in_array("$one",$one_array)){<<<
echo"one present!<br />";
>>> if(in_array("$two",$two_array)){<<<
echo"two present!";
}
else{
echo"two missing!";
}
}
else{
echo"one missing!";
}
?>
php code: <?php
$one = "monkey";
$two = "baguette";
$one_array = file("one_list.txt");
$two_array = file("two_list.txt");
>>>if (in_array($one, $one_array)){<<<
echo"one present!<br />";
>>> if(in_array($two, $two_array)){<<<
echo"two present!";
}
else{
echo"two missing!";
}
}
else{
echo"one missing!";
}
?>
And if the above works you can simplify it:
php code: <?php
if ( in_array("monkey", file("one_list.txt") ) ) { echo"one present!<br />";
if( in_array("baguette", file("two_list.txt") ) ){ echo"two present!"; }
else{ echo"two missing!"; }
} else { echo"one missing!"; }
?>
RE: PHP Array Help by prashker on 02-19-2009 at 09:16 PM
quote: Originally posted by matty
I didn't know the file function read the entire contents of the file to an array...
that's exactly what file() does , puts each line into an array
quote: Originally posted by matty
Or better yet why are you putting your variables in ""
it doesn't matter
RE: PHP Array Help by matty on 02-19-2009 at 09:21 PM
quote: Originally posted by SonicSam
quote: Originally posted by matty
Or better yet why are you putting your variables in ""
it doesn't matter
i r nub i guesses
RE: PHP Array Help by prashker on 02-19-2009 at 09:42 PM
php code: <?php
$one = "monkey";
$two = "baguette";
>>> $one_array = array_map('rtrim',file('one_list.txt')); <<<
>>> $two_array = array_map('rtrim',file('two_list.txt')); <<<
if (in_array($one,$one_array)) {
echo"one present!<br />";
if(in_array($two,$two_array)){
echo"two present!";
}
else{
echo"two missing!";
}
}
else{
echo"one missing!";
}
?>
rtrim — Strip whitespace (or other characters) from the end of a string
array_map — Applies the callback to the elements of the given arrays
i won the game
http://sonicsam.net/quest.php
RE: PHP Array Help by stoshrocket on 02-19-2009 at 10:51 PM
quote: Originally posted by SonicSam
php code: <?php
$one = "monkey";
$two = "baguette";
>>> $one_array = array_map('rtrim',file('one_list.txt')); <<<
>>> $two_array = array_map('rtrim',file('two_list.txt')); <<<
if (in_array($one,$one_array)) {
echo"one present!<br />";
if(in_array($two,$two_array)){
echo"two present!";
}
else{
echo"two missing!";
}
}
else{
echo"one missing!";
}
?>
rtrim — Strip whitespace (or other characters) from the end of a string
array_map — Applies the callback to the elements of the given arrays
i won the game
http://sonicsam.net/quest.php
YAY! You win! Have a cookie
Thankles very much Sam! =)
|