Jump to content

Comparing output of array


herghost

Recommended Posts

Hi all

 

I am having massive problems comparing the out put of array, basically my end result is to choose a selected option on a drop down in a form.

 

I am trying to compare the output of ['allow-nether'] which is either true or false in my file.

 

Here is what I have tried

 

//code to get file contents

 

$file_handle = fopen("saves/server.properties", "rb");

$vars = array();
while (!feof($file_handle) )
{
   $line_of_text = fgets($file_handle);
   $parts = explode('=', $line_of_text);
  
   //if date not required
   if ( !isset($parts[1]) )
   {
      continue;
   }
   $vars[$parts[0]] = $parts[1];
}

 

<?php
//allow nether =false in file

echo "1 " .$vars['allow-nether'];

if ( $vars['allow-nether'] == true ) { echo '<br>2 selected="selected"';} 
if ( $vars['allow-nether'] == false ) { echo '<br>3 selected="selected"';}
if ( $vars['allow-nether'] == "true" ) { echo '<br>4 selected="selected"';}
if ( $vars['allow-nether'] == "false" ) { echo '<br>5 selected="selected"';}

if ( $vars['allow-nether'] === true ) { echo '<br>6 selected="selected"';} 
if ( $vars['allow-nether'] === false ) { echo '<br>7 selected="selected"';}
if ( $vars['allow-nether'] === "true" ) { echo '<br>8 selected="selected"';}
if ( $vars['allow-nether'] === "false" ) { echo '<br>9 selected="selected"';}
?>

 

which outputs:

 

1 false 
2 selected="selected"

 

and if I set the file to allow-nether=true

 

<?php
//allow nether =true in file

echo "1 " .$vars['allow-nether'];

if ( $vars['allow-nether'] == true ) { echo '<br>2 selected="selected"';} 
if ( $vars['allow-nether'] == false ) { echo '<br>3 selected="selected"';}
if ( $vars['allow-nether'] == "true" ) { echo '<br>4 selected="selected"';}
if ( $vars['allow-nether'] == "false" ) { echo '<br>5 selected="selected"';}

if ( $vars['allow-nether'] === true ) { echo '<br>6 selected="selected"';} 
if ( $vars['allow-nether'] === false ) { echo '<br>7 selected="selected"';}
if ( $vars['allow-nether'] === "true" ) { echo '<br>8 selected="selected"';}
if ( $vars['allow-nether'] === "false" ) { echo '<br>9 selected="selected"';}
?>

 

Gives:

 

1 true 
2 selected="selected"

 

 

What am I doing wrong?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/255012-comparing-output-of-array/
Share on other sites

Your values in the file are strings AND your code is not removing the end-of-line (EOL) character(s), so your values are actually "trueEOL" and "falseEOL" and the string comparisons don't match and the string replace still leaves the EOL character(s).

 

You need to trim the data or otherwise remove the EOL character(s) before you use the data.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.