• 0

Change foreach statement into just where statement?


Question

I'm just learning PHP now so I don't know of all the possibilities and stuff. If I have this in the code:


<?php foreach ($languages as $language) { ?>

Bunch of fields here

<?php } ?>
[/PHP]

That returns a bunch of fields for every language there is. In this case, there's two languages, 1 and 2. How do I change that foreach statement so it only returns where $languages = "2"? I tried messing with WHERE but had no luck. I don't even know if WHERE could be used the way I tried to. lol

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

There's no such thing as a "where" statement in PHP. If I understand correctly, you have an array called $languages and you're looking for the element with index 2. That's pretty easy:

&lt;?php
$language = $languages[2];
?&gt;

I suggest you read some more about arrays in PHP. The documentation is always a very good starting point. ;)

If you meant that you where looking for the element which equals 2 in your array, use array_search().

  • Like 3
Link to comment
Share on other sites

  • 0

There's no such thing as a "where" statement in PHP. If I understand correctly, you have an array called $languages and you're looking for the element with index 2. That's pretty easy:

&lt;?php
$language = $languages[2];
?&gt;

I suggest you read some more about arrays in PHP. The documentation is always a very good starting point. ;)

If you meant that you where looking for the element which equals 2 in your array, use array_search().

Basically this +rep.

WHERE could be used as part of an SQL query if you were using something like mySQL.

Example:

 SELECT * FROM table_name WHERE id=1

Or something similar to that (I'm not an expert with SQL Schemas :<)

Link to comment
Share on other sites

  • 0

There's no such thing as a "where" statement in PHP. If I understand correctly, you have an array called $languages and you're looking for the element with index 2. That's pretty easy:

&lt;?php
$language = $languages[2];
?&gt;

I suggest you read some more about arrays in PHP. The documentation is always a very good starting point. ;)

If you meant that you where looking for the element which equals 2 in your array, use array_search().

Sweet, that works! Thanks a bunch. I definitely need to mess around with php more. That's the best way to learn.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.