JasonDavis JasonDavis - 5 months ago 161
JSON Question

Filter Items from JSON Objects in PHP

I have this Function/Method for filtering out a Blacklist of words in an

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
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


Except you want a solution specifically for object if not you don't need it

you are calling

  $obj = json_decode($out);

That is why you are getting an object .. if you call

 $obj = json_decode($out,true);

When second argument is TRUE, json_decode returned objects will be converted into associative arrays.

The response would be a array see http://php.net/manual/en/function.json-decode.php for documentation