0

I have an array of objects when is being pulled in from a database and would like to return each object where the 'status' field is equal to 'No Answer':

array (size=21)
0 => 
  object(Customer)[4]
  public 'id' => string '12345' (length=6)
  public 'date' => string '2014-02-16' (length=10)
  public 'first_name' => string 'Jane' (length=5)
  public 'last_name' => string 'Doe' (length=4)
  public 'email' => string '[email protected]' (length=21)
  public 'phone' => string '01782111444' (length=11)
  public 'status' => string 'No Answer' (length=14)
1 => 
object(Customer)[5]
  public 'id' => string '12346' (length=6)
  public 'date' => string '2014-02-19' (length=10)
  public 'first_name' => string 'John' (length=4)
  public 'last_name' => string 'Smith' (length=9)
  public 'email' => string '[email protected]' (length=24)
  public 'phone' => string '01606555666' (length=11)
  public 'status' => string 'Left Message' (length=12)

It seems like I would need to use array_filter() but cant seem to get it to work

3
  • array_filter() seems right to me. Can you show us the invocation you've been trying? Commented Mar 10, 2014 at 19:10
  • 4
    Is this not a criterion you could add to a database query? How is it stored/accessed? Commented Mar 10, 2014 at 19:11
  • I want to display different status' separated by tabs so rather than keep going back to the database with 5/6 variations of the same query I thought it would be better to select all then filter the array? Commented Mar 10, 2014 at 19:23

1 Answer 1

2

Like @Wiseguy commented, you should be doing this in your query, not the code.

See array_filter():

array_filter($array, function($i) {
  if(strtolower($i->status) == 'no answer') { return true; }
  else { return false; }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Works perfect, thanks. Is it not better to filter an array of results then, than make multiple calls to the database?
@user1105056 Your question implies that you're throwing away the results that are filtered out. If you're making use of them elsewhere, then feel free to ignore the suggestions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.