473,606 Members | 2,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_intersec t(";
for ($i=0; $i<count($value s); $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 5102
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_intersec t(";
for ($i=0; $i<count($value s); $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_intersec t', $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_intersec t', $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
1928
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, axis): """ elementary routine to strip the edge cells of an array for the given direction. """
53
5685
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 difficult to know what is going on. One of these Order Forms you can see here... http://www.cardman.co.uk/orderform.php3
0
1195
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 GetObjectData(). All classes use the attribute and have a default constructor. When deserializing from an xml string that represents a complex object, I am getting NullReferenceExceptions that seem to be related to the arrays. The object is created...
7
5542
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, with different file names each time. So i'm writing into the code to ask the user how many files, and what their names are. From each we'll read in 2 lines, then do some math using all of those lines. Then do it again on another set of lines. ...
1
3835
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 from 0.; I found the intersect array $a3 have the correct values but keep the keys of $a1. When I use
12
9605
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
3090
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 should be 5. However it return "Array".... Does anyone know why?
33
7151
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 array, so im inputting the dimensions of those myself. The problem is that the output array (C=A*B) has as many rows as A and as many columns as B. I would think of initialising C with:
4
4030
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 and open an unknown workbook that contains the data that I need to imput into my current workbook, once the unknown workbook is opened it should automatically select my range (number of columns known, but number of rows unknown- the number of rows...
0
8045
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7981
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8467
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8462
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8320
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6803
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5994
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3952
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
1315
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.