You are here: Re: Finding elements of an array in another multi-dimensional array « PHP Programming Language « IT news, forums, messages
Re: Finding elements of an array in another multi-dimensional array

Posted by Erwin Moller on 02/01/07 11:28

Sandman wrote:

> Hi,
> So I have 2 arrays:
> one contains userids. It may look like:
> user_id[0] => 12,
> user_id[1] => 30,
> user_id[2] => 43
>
> The other is a multi-dimensional array with fields like:
> user_info [0] => Array
> (
> [user_id] => 13
> [user_flag] => 1
> [url] => http://www.example.com?index,0
> )
> user_info[1] => Array
> {
> Array
> (
> [user_id] => 120
> [user_flag] => 1
> [address] => 1234 Main St, Anytown, USA
> [url] => http://www.yahoo.com
> )
> user_info[2] => Array
> {
> Array
> (
> [user_id] => 130
> [user_flag] => 1
> [address] => 134 Main St, Anytown, USA
> [url] => http://www.google.com
> )
>
>
> I need to find all elements in user_info, where user_info[]['user_id']
> == user_id[] for every element of user_id.
>
> Is there a quick and easy function to do this? My current loop looks
> like:
> for ($i=0; $i<count($user_id); $i++) {
> for ($j=0; $j<=count($user_info); $j++) {
> if ($user_info[$j]["user_id"] == $user_id[$i]) {
> $newarray[$i] = $user_info[$j];
> break;
> }
> }
> }
>
> This is crappy though and I'd love to speed it up. Suggestions
> welcome.
>
> Thanks!
> EL

Hi Sandman,

In my humble opinion that code is not crappy at all.
Maybe you can find (a combination of) array functions, or even regexp if you
feel really adventurous, that do the same job, but your approach is clear
and easy to understand for anybody that reads the code later (including
you).
This is just my opinion, but if I should make a list for code, this is the
order of importance:
1) clearity of code
2) speed of the code
3) length of the code

And I only care for the speed of the code if there is a reason to worry,
like repetive SQL-queries.

Regards,
Erwin Moller

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация