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

Problems With Arrays!!!

P: 10
Hi,

I'm having a problem with array's functions

Expand|Select|Wrap|Line Numbers
  1. //Making the list of the differents arrays
  2. if($cpt_couleur+1<count($_POST["couleur"])){
  3. $liste_tableaux_couleur.=${"tab_resultat_couleur".$cpt_couleur}.",";
  4. }
  5. else{
  6. $liste_tableaux_couleur.=${"tab_resultat_couleur".$cpt_couleur};
  7. }
  8.  
  9. //Keeping only the numbers that are present in each table
  10. if(count($_POST["couleur"])>1){
  11. $tab_resultat_couleur=array_merge(array_intersect($liste_tableaux_couleur));
  12. }
  13. else{
  14. $tab_resultat_couleur=$liste_tableaux_couleur;
  15. }
The problem is that array_intersect needs 2 parameters to work... I've
also tried with dynamic variables but I think I code the wrong thing 'cause it
didn't solve my problem... Is there any way to just write the content of
the variable $liste_tableaux_couleur in the function array_intersect...
something that would execute the line like :
$tab_resultat_couleur=array_merge(array_intersect( $tab_resultat_couleur0,$tab_resultat_couleur1,$tab _resultat_couleur2));
...

Thanks a lot!!!!

Marie-Hélène
Oct 8 '08 #1
Share this Question
Share on Google+
9 Replies


Markus
Expert 5K+
P: 6,050
Hey there, Marie-Hélène.

When posting code on the forums, be sure to use [code] tags. [code] .. code goes here [/code]. If you neglect to do so, you will be given a warning and possibly a temporary ban. Please read the Posting Guidelines on How To Ask A Question

Markus.
Oct 8 '08 #2

Dormilich
Expert Mod 5K+
P: 8,639
I think the problem lies in $liste_tableaux_couleur. make sure this really is an array, otherwise the array functions won't work. right now it seems to be a string (you can check this with var_dump()).

array_intersect() needs 2 input parameters to work. currently you have only one.

regards

PS: add an array element: $your_array[] = $new_element;
Oct 8 '08 #3

P: 10
I just want to find a way to write the content of the variable $liste_tableaux_couleur in my function
[PHP]array_intersect()[/PHP] to have something like [PHP]array_intersect($table1,$table2,$table3...)[/PHP]... all the names of tables are in my variable [PHP]$liste_tableaux_couleur [/PHP] but I can't do [PHP]array_intersect($liste_tableaux_couleur)[/PHP], because I get the error that the function needs 2 parameters, is there a way to write the values of my variable directly into the parenthesis of the function????

[PHP]$liste_tableaux_couleur[/PHP] is not an array but a string variable that contains, by example, differents names of tables like [PHP]"$table1,$table2,$table3"[/PHP]

The variable [PHP]$liste_tableaux_couleur[/PHP] is in a loop because it can contains 2 table or 3 or 4...etc... depending on what the users chose.

Thanks a lot,

Marie-Hélène
Oct 8 '08 #4

Dormilich
Expert Mod 5K+
P: 8,639
I see 2 (at least) posibilities
#1 merge all arrays an make the result unique
[PHP]$liste_tableaux_couleur = array();
while ($count < $limit) {
$liste_tableaux_couleur = array_merge($liste_tableaux_couleur, ${"table" . $count});
$count++;
}
$tab_resultat_couleur = array_unique($liste_tableaux_couleur);[/PHP]
# 2 use the eval() function
[PHP]$expression = '$tab_resultat_couleur = array_merge( array_intersect(' . $liste_tableaux_couleur . ') )';
eval( $expression );[/PHP]
regards
Oct 8 '08 #5

P: 10
I use your second proposition :

[PHP]eval('$tab_resultat_couleur=array_merge(array_inte rsect('.$liste_tableaux_couleur.'))');[/PHP]

but I get the error Parse error: syntax error, unexpected $end in ...(363) : eval()'d code on line 1

Do you have any idea?
Oct 8 '08 #6

Dormilich
Expert Mod 5K+
P: 8,639
oh, I forgot the ; at the end of the string. ...stupid me
[PHP]
eval( '$tab_resultat_couleur = array_merge(array_intersect(' . $liste_tableaux_couleur . '));' );[/PHP]
Oct 8 '08 #7

pbmods
Expert 5K+
P: 5,821
Heya, Marie-Hélène.

Try this, instead:

Expand|Select|Wrap|Line Numbers
  1. $tab_resultat_couleur = array_merge(call_user_func_array('array_intersect', $liste_tableaux_couleur));
  2.  
http://php.net/call_user_func_array
Oct 9 '08 #8

P: 10
Wow Dormilich, that works very well!!! I didn't know we can use the function eval in php like in javascript.... Thanks a lot for your help!!!

Have a nice day!

Marie-Hélène
Oct 9 '08 #9

Dormilich
Expert Mod 5K+
P: 8,639
I didn't know we can use the function eval in php like in javascript....
Salut Marie-Hélène,

the PHP developers had obviously a good reason to call their function eval()..... *g*

Dormi ← glad to be of help
Oct 9 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.