开发者

fastest way to get element values from an array object in php

I have something like this

Array
(
    [0] => stdClass Object
        (
            [CustomerID] => 14
            [Email] => joe.blogs@example.com
            [LastName] => Blogs
            [BirthDayOfMonth] => 29
            [Gender] => 
            [Occupation] => 
            [SendSpecialOffers] => 1
            [SendReminderNotes] => 1
        )

    [1] => stdClass Object
        (
            [CustomerID] => 1460
            [Email] => example@example.com
            [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', 'example@example.com'

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?


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;
}


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.


array_filter is best..see the examples on manual

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜