2

I have something like this

Array
(
    [0] => stdClass Object
        (
            [CustomerID] => 14
            [Email] => [email protected]
            [LastName] => Blogs
            [BirthDayOfMonth] => 29
            [Gender] => 
            [Occupation] => 
            [SendSpecialOffers] => 1
            [SendReminderNotes] => 1
        )

    [1] => stdClass Object
        (
            [CustomerID] => 1460
            [Email] => [email protected]
            [LastName] => Example
            [BirthDayOfMonth] => 5
            [Gender] => F
            [Occupation] => 
            [SendSpecialOffers] => 1
            [SendReminderNotes] => 1
        )
);

I would like get Email address of each separated by commas, something like this

'joe.blogs@example', '[email protected]'

I know i could iterate it through foreach but i got a really big list, is there anyway to do it faster? thanks

Now, how can i remove the indexes based some email addresses?

2
  • 11
    I hope those email address owners don't mind you posting them here... ;) Commented Jul 26, 2011 at 10:36
  • "but i got a really big list" - the perils of ORM! Commented Jul 26, 2011 at 10:39

3 Answers 3

6

You can do this with array map and a function but this will also iterate your array

echo implode(',',array_map('getEmail',$array));

function getEmail($obj)
{
  return $obj->Email;
}
Sign up to request clarification or add additional context in comments.

Comments

3

The simplest solution would indeed be a foreach() to iterate over all the items of your array ; adding, for each item, the email to a another resulting array.


Maybe you could replace the foreach by a call to array_walk(), but it probably wouldn't change much :

  • You wouldn't loop in PHP, as array_walk is coded in C (could be a bit faster than foreach -- not sure, though)
  • But a function would be called for each item, instead of just a couple of PHP instructions.

You'd have to benchmark, to see if there is a significant difference in your specific case -- but I personnaly would go for the foreach, without thinking much more.

Comments

0

array_filter is best..see the examples on manual

1 Comment

array_filter() returns an array of elements, that are treated "valid" according a given callback. Can you explain, how do you want to use it to extract an object property from every array element?

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.