0

I have an unsorted and dynamically changing array in php where I have values stored as in "blue", "red", "green". The rest of the array consists of strings like "wet", "dry", "humid". Now I want to print respective titles before printing the strings to my html page. How can I achieve this?

Above code produces problems since if else fires, before all three colors have been echoed out, it will print the heading again.

Edit:

Array: blue, humid, green, wet, dry, red.

echo "<h3>Color</h3><br>";
foreach($array as $value){
if ($value == "blue" || $value == "red" || $value == "green") {
    echo $value;
}
echo "<h3>Weather</h3><br>";
foreach($array as $value){
if ($value != "blue" || $value != "red" || $value != "green") {
    echo $value;
}
3
  • What are respective titles? Commented Sep 27, 2012 at 13:11
  • <h1>Color</h1> and <h2>Weather</h2>. Commented Sep 27, 2012 at 13:12
  • It would really help if you'd provide us with your array structure. Commented Sep 27, 2012 at 13:15

3 Answers 3

1

Yes, because your boolean expression isn't correct. It should be

if($file == 'blue' || $file == 'red' || $file == 'green')

The way you've written it it will be equal to

if($file == 'blue' || true || true)

And with the logical OR the expression is TRUE if only one of the variables equals TRUE.
Edit
On a second reading, echo the heading before the foreach loop.

Sign up to request clarification or add additional context in comments.

3 Comments

yea I know about the boolean expression, sorry for that, it is not the issue here. The point is that the script WORKS FINE - but the if the array is UNSORTED, it does produce the heading more than once. I need to sort the thing before hand and split it into 2 different arrays I guess.
@StephanKristyn With the code you have shown, this certainly is an issue. As for the rest of your code; how are we to know?? You didn't show it.
Echoing the (colors) heading before the foreach won't help either, in case the array is unsorted, think about it. Item's will get left out and then get printed without (colors) heading after the second (weather) heading was printed in the else condition.
0

if statement syntax is wrong. It needs to be if ($file == "blue" || $file == "red" || $file == "green") {

echo the heading echo "<h1>Colours</h1>"; before the foreach loop, like this:

       echo "<h1>Colours</h1>";      
       foreach($file_list as $file) {

      if ($file == "blue" || "red" || "green") {        
         echo $file;
        }
      else { ...}

2 Comments

<h2>Weahter</h2> prints in else condition, but if green hasn't been echoed yet, you will have a problem.
you would need to show the entire code(atleast the if-else statement), for us to figure out your question. Also, the structure for $file_list
0

I got it myself. Changing the logical ORs in the second foreach loop does print out only the weather conditions as expected.

echo "<h3>Color</h3><br>";
foreach($array as $value){
if ($value == "blue" || $value == "red" || $value == "green") {
    echo $value;
}
echo "<h3>Weather</h3><br>";
foreach($array as $value){
if ($value != "blue" && $value != "red" && $value != "green") {
    echo $value;
}

1 Comment

well done, now you can mark it as an answer - nothing wrong with that ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.