Page 1 of 1

multi dimensional array

Posted: Sat Apr 25, 2009 12:27 pm
by jazz090
i got this array:

array = array ("name" = $name, "email" = $email),array ("name" = $name, "email" = $email),array ("name" = $name, "email" = $email),.....

is there any function that will allow me to extract all "name" into an array?

Re: multi dimensional array

Posted: Sat Apr 25, 2009 12:29 pm
by Benjamin

Code: Select all

 
function getNames($array) {
    $names = array();
 
    foreach ($array as $k => $data) {
        if (is_array($data) && isset($data['name'])) {
            $names[] = $data['name'];
        }
    }
 
    return $names;
}
 

Re: multi dimensional array

Posted: Sat Apr 25, 2009 12:32 pm
by jazz090
anyway u can do this w/o loop?(the whole reason i asked)

Re: multi dimensional array

Posted: Sat Apr 25, 2009 12:36 pm
by Benjamin
I don't think you can. Are you worried about performance?

Re: multi dimensional array

Posted: Sat Apr 25, 2009 12:38 pm
by jazz090
yh cos i already have the loop which makes that array in the first place but i dont want to run my code in there

Re: multi dimensional array

Posted: Sat Apr 25, 2009 12:43 pm
by Benjamin
jazz090 wrote:but i dont want to run my code in there
You can call the function I wrote from anyplace, so you wouldn't have to.

Re: multi dimensional array

Posted: Sat Apr 25, 2009 12:44 pm
by jazz090
safe