/    Sign up×
Community /Pin to ProfileBookmark

Filter down into the Data in arrays

Hi. Help Please.

I just can’t seem to get my head around this:
one array of jobs needs to be filtered down to the desired info



[code=php]
//default: no filter
$alljobs;

//filter one: job types
//if filter one is the ONLY active filter, code to get the jobs by field TYPE (done)
$alljobs=$jobtypes;

//filter two: job cities
//if filter two is active is the ONLY active filter, code to get the jobs by field CITIES (done)
$alljobs=$jobcities;

//now if BOTH filters are active (job cities AND job types)
//stuck here!!!
$alljobs=?????????

//there are 4 filters…giving me a total of 15 iterations of filters that can be ON at any time

[/code]

I’ve used if, elseif, etc…it works…but
I get repeated results when I do
(here is my problem)
array_merge($jobtypes,$jobcities, …);

I guess merging them is not the answer…

please help!
thank you for your help in advance.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@gomisuteauthorDec 23.2007 — Guess not many people on this forum wanna answer array questions.

Any suggestions is appreciated.
Copy linkTweet thisAlerts:
@holidayDec 24.2007 — OK dude you're a little hard to follow (read your own post). Retype the whole thing, and tell me exactly what you are trying to do, and I promise I will do everything in my power to solve your problem. Because ppl on this forum love answering array questions.


HolidayrekcaH
Copy linkTweet thisAlerts:
@gomisuteauthorDec 24.2007 — OK dude you're a little hard to follow (read your own post). Retype the whole thing, and tell me exactly what you are trying to do, and I promise I will do everything in my power to solve your problem. Because ppl on this forum love answering array questions.
[/QUOTE]


Fair Enough...I'll be more clear and I look forward to your answers.
---



I have an array of jobs (each job is a row in a table).

Each row has fields (name, date, type, city, industry, etc...)

They're displayed via a search:

No search fields gives me an array of jobs and all jobs are displayed

but

if any one (or all) of the 4 filter fields (Type, City, Industry, Date) are entered, one by one, the jobs are filtered and only the remaining jobs that have a match to those filter fields are displayed.


---
Example

Jobs in DB:

Job1(type3, city2, industry2);

Job2(type1, city3, industry3);

Job3(type2, city6, industry4);

Job4(type1, city3, industry3);

Job5(type1, city6, industry3);

.

.

.

Jobn(............)
----



Filter Fields entered:

FFields1(type1, city3, industry3)

Filtered Results:

Job-Results (Job2, Job4);


----
Because I have 4 different filter fields than can be input,

I have a total of 15 different ways someone can filter the job list.

(4C4 + 4C3 + 4C2 + 4C1 = 15)
---


My Solution (the long and maybe inefficient way):

(I was hoping for assistance from this forum sooner, but I was in need of a solution right away, so...I dug into it myself and I came up with the following):

I've got 15 sets of 'if elseif' statements!

I match the jobs and I get what I want...but since they are all in arrays

I was hoping there is a better way to do this.

Thank you.
Copy linkTweet thisAlerts:
@holidayDec 24.2007 — What kind of database are you using? Are you using MySQL? You should be able to filter the results when you make the query. Unless you're not really using a database, or for some reason you can only deal with it after it's in an array.

Here's how you would set search parameters for a MySQL database query
[code=php]
<?php

//do MySQL (database) query
$result = mysql_query("SELECT * FROM jobs WHERE city='city2' AND (industry='industry3' || industry='industry4') AND type='type1'");

//echo query results
while($row = mysql_fetch_array($result)) {
echo $row['type'] . ", " . $row['city'] . ", " . $row['industry'] . ", " . $row['date'] . "<br />n";
}

?>
[/code]


Here's how you can filter arrays
[code=php]
<?php

//set $jobs array
$jobs = array();

//set job array values
$jobs[] = array("type3", "city2", "industry2", "date3");
$jobs[] = array("type1", "city3", "industry3", "date2");
$jobs[] = array("type2", "city5", "industry4", "date1");
$jobs[] = array("type1", "city3", "industry3", "date5");
$jobs[] = array("type1", "city5", "industry3", "date4");

//set search parameters
$search[0] = array("type1", "type2");
$search[1] = array("city5");
$search[2] = array();
$search[3] = array("date4", "date1");

//remove empty parameters from search array
$search = array_filter($search);

//set $jobs_filtered array
$jobs_filtered = array();

//filter jobs array
foreach($jobs as $job) {
//set kill to false
$kill = false;

//go through search keys
foreach($search as $key => $value) {
//if job key not found in search keys, set kill to true.
if(!in_array($job[$key], $value)) {
$kill = true;
}

}

//if kill == false, add job to filtered jobs array.
if(!$kill) {
$jobs_filtered[] = $job;
}

}

//echo filtered results
foreach($jobs_filtered as $var) {
echo $var[0] . ", " . $var[1] . ", " . $var[2] . ", " . $var[3] . "<br />n";
}

?>
[/code]


and here is the same but as a function
[code=php]
/Create Function: filter_array
function filter_jobs($jobs, $search) {
//remove empty parameters from search array
$search = array_filter($search);

//set $jobs_filtered array
$jobs_filtered = array();

//filter jobs array
foreach($jobs as $job) {
//set kill to false
$kill = false;

//go through search keys
foreach($search as $key => $value) {
//if job key not found in search keys, set kill to true.
if(!in_array($job[$key], $value)) {
$kill = true;
}

}

//if kill == false, add job to filtered jobs array.
if(!$kill) {
$jobs_filtered[] = $job;
}

}

//return filtered array
return $jobs_filtered;
}

//set $jobs array
$jobs = array();

//set job array values
$jobs[] = array("type3", "city2", "industry2", "date3");
$jobs[] = array("type1", "city3", "industry3", "date2");
$jobs[] = array("type2", "city5", "industry4", "date1");
$jobs[] = array("type1", "city3", "industry3", "date5");
$jobs[] = array("type1", "city5", "industry3", "date4");

//set search parameters
$search[0] = array("type1", "type2");
$search[1] = array("city5");
$search[2] = array();
$search[3] = array("date4", "date1");

$filtered = filter_jobs($jobs, $search);
[/code]


I hope this answers you question, if not, or if you have any more questions, just ask ?


Holiday
Copy linkTweet thisAlerts:
@gomisuteauthorDec 26.2007 — Hi

Thanks.

That looks great.

I use MySql.

I think your function is just what I need.

I will tweak it to my requirements and use it.

I appreciate your help.

All the best.
×

Success!

Help @gomisute spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...