2

I've never really used PHP arrays before as I've just used a database in the past so I'm broadening my horizons a little. Basically I have a simple nested array where each element has a 'name' and some other values. I'm trying to search through the array, which is part of an object. I've looked through a number of previous questions on here and can't get it working, although in the other cases there haven't been objects involved. I've been trying to use a 'needle/haystack' type example but haven't got it working yet.

So in my People class we have among other things:

public $peopleArray;  // this is the array and will be protected once working

// and this is the example search function im trying to modify
public function findPerson($needle, $haystack)
{
    foreach($haystack as $key=>$value)
    {
        if(is_array($value) && array_search($needle, $value) !== false)
        {
            return $key;
        }
    }
    return 'false';
}

And then to call this I currently have:

$searchResult = $People->findPerson('Bob',$people->peopleArray,'name');

I'm not sure if I'm just confusing myself with what the $needle and $value - I need to pass the name value in the search function, so I did have $value in the function arguments, but this still returned nothing. Also I'm not 100% on whether '$key=>$value' needs modifying as $key is undefined.

Thanks in advance for any help.

Addition - print_r of the array:

Array ( [0] => Person Object ( [id:protected] => 1 [name] => Bob [gender] => m ) 
[1] => Person Object ( [id:protected] => 2 [name] => Denise [gender] => f ) 
[2] => Person Object ( [id:protected] => 3 [name] => Madge [gender] => f ) ) 
3
  • 2
    Could you add a print_r() of the $peopleArray to your question Commented Dec 10, 2012 at 11:23
  • array_search($needle, $value) !== false should be array_search($needle, $value) != false) Commented Dec 10, 2012 at 11:26
  • Thanks guys - Ive added a print_r of the array Commented Dec 10, 2012 at 11:35

1 Answer 1

2

Ok this would be a lot easier if you had to post an example array, but I'm going to give this question a shot.

To me it seems you are looping through a 2D array (nested array).

I would loop through multidimensional arrays like such:

for($array as $key => $2ndArray){
    for($2ndArray as $2ndKey => $value){
        if($value == $needle){
           return true;
        }
    }
}

Hope this helps

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Luke. One of things that's confusing me is the syntax for this with relation to the array being an attribute of an object. So here $array would be $this->peopleArray, but it doesn't seem to like that. Also I haven't come across anything like the '$key => $2ndArray' part before - would I have that simply as '$key => $name'?
Ok I've got the search working now. a bit of tinkering and this worked - thanks Luke
glad to see you got it working. I believe this will work for most objects and arrays. Post any changes you made here in the comments, and I will edit the answer.

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.