0

Let's say I have an array of objects.

<?php

$people = array();
$people[] = new person('Walter Cook');
$people[] = new person('Amy Green');
$people[] = new person('Irene Smith');

How can I search an object in this array for a certain instance variable? For example, let's say I wanted to search for a person object with the name of "Walter Cook".

Thanks in advance!

4
  • us2.php.net/in_array Commented Nov 8, 2013 at 2:04
  • @l19 in_array() looks for exact matches, it can't search for objects that have certain properties. Commented Nov 8, 2013 at 2:09
  • 1
    There's nothing built-in that does this, just write a foreach loop that compares the person's name, and breaks when it finds it. Commented Nov 8, 2013 at 2:10
  • @l19 The in_array function is not what I am looking for. This will only return a boolean, but the object itself. Commented Nov 8, 2013 at 2:10

4 Answers 4

4

It depends of the person class construction, but if it has a field name that keeps given names, you can get this object with a loop like this:

for($i = 0; $i < count($people); $i++) {
    if($people[$i]->name == $search_name) {
        $person = $people[$i];
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Here is:

$requiredPerson = null;

for($i=0;$i<sizeof($people);$i++)
{
   if($people[$i]->name == "Walter Cook")
    {
        $requiredPerson = $people[$i];
        break;
    }

}

if($requiredPerson == null)
{
    //no person found with required property
}else{
    //person found :)
}

?>

Comments

1

Assuming that name is a public property of the person class:

<?php

// build the array of objects
$people = array();
$people[] = new person('Walter Cook');
$people[] = new person('Amy Green');
$people[] = new person('Irene Smith');

// search name
$searchName = 'Walter Cook';

// ascertain the presence of the name in the array of objects
$isMatch = false;

foreach ($people as $person) {
    if ($person->name === $searchName) {
        $isMatch = true;
        break;
    }
}

// alternatively, if you want to return all matches into 
// a new array of $results you can use array_filter
$result = array_filter($people, function($person) use ($searchName) {
    return $person->name === $searchName;
});

hope this helps :)

1 Comment

sorry, I just saw the question has already been marked as answered while I was adding this!
0

well you could try this inside your class

    //the search function
function search_array($array, $attr_name, $attr_value) {
    foreach ($array as $element) {
        if ($element -> $attr_name == $attr_value) {
            return TRUE;
        }
    }
    return FALSE;
}

//this function will test the output of the search_array function
function test_Search_array() {
    $person1 = new stdClass();
    $person1 -> name = 'John';
    $person1 -> age = 21;

    $person2 = new stdClass();
    $person2 -> name = 'Smith';
    $person2 -> age = 22;
    $test = array($person1, $person2);
    //upper/lower case should be the same
    $result = $this -> search_array($test, 'name', 'John');
    echo json_encode($result);
}

Comments

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.