By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,302 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,302 IT Pros & Developers. It's quick & easy.

Finding elements of an array in another multi-dimensional array

P: n/a
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

Feb 1 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Feb 1 '07 #2

P: n/a
Tom
On Feb 1, 6:28 am, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
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
This might be cleaner, also less confusing since you don't have to do
count( )'s:
<?php
foreach ( $user_id AS $id ) {
foreach ( $user_info AS $array ) {
if ( $array['user_id'] == $id ) {
// your code here
}
}
}
?>

Feb 1 '07 #3

P: n/a
Thanks Erwin. For some reason that script (which does more than just
the loop) was taking a very long to load. After posting here, I stuck
microtime() before and after the loop, but found that even when
user_info[] had 10000 entries it took less than 10 ms. So the slowness
is elsewhere.

So I'm going to leave my loop like this, but look elsewhere for the
slowness. Back to the drawing board

Thanks everyone.
S
On Feb 1, 3:28 am, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
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

Feb 1 '07 #4

P: n/a
On Thu, 01 Feb 2007 10:18:17 -0800, Sandman <en*************@hotmail.com
wrote:
On Feb 1, 3:28 am, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>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

Thanks Erwin. For some reason that script (which does more than just
the loop) was taking a very long to load. After posting here, I stuck
microtime() before and after the loop, but found that even when
user_info[] had 10000 entries it took less than 10 ms. So the slowness
is elsewhere.

So I'm going to leave my loop like this, but look elsewhere for the
slowness. Back to the drawing board

Thanks everyone.
S
You could probably speed up the execution time by using better queries on
your database. It seems like a JOIN could take care of your PHP processing
there, but I may be/probably am wrong.

--
Curtis, http://dyersweb.com
Feb 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.