473,412 Members | 4,594 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

array_intersect with unknown number of arrays

Hi

I have a question regarding array_intersect() in PHP 4.3. There are several
arrays created on the fly, such as:

$values['apples'];
$values['bananas'];
$values['pears'];

To extract only the values they have in common I tried this:

$command = "array_intersect(";
for ($i=0; $i<count($values); $i++) {
$command .= "\$values['$values[$i]'],";
}
$command = substr($command, 0, -1);
$command .= ");";
$result = eval($command);

This does not seem to work and also is quite complicated. Does somebody know
a better solution?

Thanks
Markus
Jul 17 '05 #1
2 5081
I have a question regarding array_intersect() in PHP 4.3. There are several
arrays created on the fly, such as:

$values['apples'];
$values['bananas'];
$values['pears'];

To extract only the values they have in common I tried this:

$command = "array_intersect(";
for ($i=0; $i<count($values); $i++) {
$command .= "\$values['$values[$i]'],";
}
$command = substr($command, 0, -1);
$command .= ");";
$result = eval($command);

This does not seem to work and also is quite complicated. Does somebody know
a better solution?


The user comments in the docs for array_intersect suggest using
call_user_func_array() to pass an arbitrary number of arguments.
Looks like you should be able to use:

$result = call_user_func_array( 'array_intersect', $values );

(untested, got to catch a bus!)

---
Steve

Jul 17 '05 #2
Steve wrote:
[...]
The user comments in the docs for array_intersect suggest using
call_user_func_array() to pass an arbitrary number of arguments.
Looks like you should be able to use:

$result = call_user_func_array( 'array_intersect', $values );


Works perfectly! I had even read this note, but as I did not understand the
examples provided I did not realize that it was what I looked for. Seems
like it's worth to have a closer look at PHP's function functions.

Thanks a lot for your input!

--
Markus
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Maarten van Reeuwijk | last post by:
Hello, I am using the Numeric package, and I need to strip edge cells off an array (dimension unknown) in an a-priori unknown direction. I implented this as follows: def el_remove_bcells(var,...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
0
by: PeterW | last post by:
I have a class which uses three other classes, two of which are used in arrays. This class is only ever used for incoming messages. All classes inherit from ISerializable and impliment...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
1
by: duzhidian | last post by:
hello all, I only use indexed arrays. When I use array_intersect function, of $a3 = array_intersect( $a1, $a2 ), I just need the vales of intersect array and let keys to be re-numberred...
12
by: Magcialking | last post by:
for example,in the function "int a(int b)", I wanna every element of array b to be dealt with, but b's length remains unkown, so what can I do?
3
by: wesley1970 | last post by:
<?php $a = array(1,2,3,4,5); $b = array(1,2,3,4,5,6,7,8); echo array_intersect($a, $b); ?> array_intersect should return the numbers of items intersect in 2 arrays. In this case, it...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
4
by: omono84 | last post by:
I know that this should be rather simple but i seem to be missing a step to get it to work. and have been unable to find a solution on the net. The aim is that I click on the open button to find...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.