For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > November 2006 > Something like "WHERE" from SQL









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Something like "WHERE" from SQL
Kenoli

2006-11-23, 6:58 pm

Is there a way to accomplish something like the "WHERE" syntax from SQL
in php? For example an equivalent to the following, which I know is
not proper php syntax.

foreach ($var as $key => $value WHERE $key != '3') {
echo "$key is $value";
}

or

implode (",", $_POST WHERE $_POST[name] != 'post');

I keep wanting to be able to do things like this.

--Kenoli

Captain Paralytic

2006-11-23, 6:58 pm


Kenoli wrote:
> Is there a way to accomplish something like the "WHERE" syntax from SQL
> in php? For example an equivalent to the following, which I know is
> not proper php syntax.
>
> foreach ($var as $key => $value WHERE $key != '3') {
> echo "$key is $value";
> }
>
> or
>
> implode (",", $_POST WHERE $_POST[name] != 'post');
>
> I keep wanting to be able to do things like this.
>
> --Kenoli


foreach ($var as $key => $value) {
if $key != '3'
echo "$key is $value";

Rik

2006-11-27, 6:58 pm

Kenoli wrote:
> Is there a way to accomplish something like the "WHERE" syntax from
> SQL in php? For example an equivalent to the following, which I know
> is not proper php syntax.
>
> foreach ($var as $key => $value WHERE $key != '3') {
> echo "$key is $value";
> }
>
> or
>
> implode (",", $_POST WHERE $_POST[name] != 'post');
>
> I keep wanting to be able to do things like this.



array_filter();
http://nl3.php.net/manual/en/function.array-filter.php

The problem here is you cannot filter on keys.

On could use array_keys(), array_filter() and then again array_flip() and
array_intersect_keys(). It's hardly better then a foreach loop however, and
a lot less readable.
--
Rik Wasmus


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com