Friday, April 20, 2012

Filter Items from JSON Objects in PHP

I have this Function/Method for filtering out a Blacklist of words in an ARRAY() using array_filter but I need to do something similar to my Objects in this code below...

JSON Objects

// Get JSON Object
$obj = json_decode($out);

// Iterate JSON Object
foreach($obj as $index => $user) {
echo $user->id;
echo $user->screen_name;
echo $user->language;
echo $user->location;
echo $user->time_zone;
echo $last_status_date;
echo $user->status->text;

// Filter out Objects that match the Blacklist

// insert remainning into database here


My current Blacklist Filter Function

public function blacklistFilter($raw_array){

//$data1 = array('Phillyfreelance' , 'PhillyWebJobs', 'web2project', 'cleanname');
$data1 = array_filter($data1, function($el) {
$bad_words = array('job', 'freelance', 'project', 'gig', 'word', 'news', 'studio');
$word_okay = true;

foreach ( $bad_words as $bad_word ) {
if ( stripos($el, $bad_word) !== FALSE ) {
$word_okay = false;

return $word_okay;

So I am curious if there is a simialr function for filtering objects as array_filter does for ARRAYS?

Ultimately my goal is to be able to pass hundreds of JSON Objects through a function and be able to filter out ones that match a set of words in the username, filter out ones that match a language, and filter ones out that match a location or time zone

No comments:

Post a Comment