Problems With Arrays!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Emmash
    New Member
    • Oct 2008
    • 10

    Problems With Arrays!!!

    Hi,

    I'm having a problem with array's functions

    Code:
    //Making the list of the differents arrays
    if($cpt_couleur+1<count($_POST["couleur"])){
    $liste_tableaux_couleur.=${"tab_resultat_couleur".$cpt_couleur}.",";
    }
    else{
    $liste_tableaux_couleur.=${"tab_resultat_couleur".$cpt_couleur};
    }
    
    //Keeping only the numbers that are present in each table
    if(count($_POST["couleur"])>1){
    $tab_resultat_couleur=array_merge(array_intersect($liste_tableaux_couleur));
    }
    else{
    $tab_resultat_couleur=$liste_tableaux_couleur;
    }
    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_c ouleur=array_me rge(array_inter sect($tab_resul tat_couleur0,$t ab_resultat_cou leur1,$tab_resu ltat_couleur2)) ;
    ...

    Thanks a lot!!!!

    Marie-Hélène
    Last edited by Markus; Oct 8 '08, 05:33 PM. Reason: added [code] tags
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    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.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      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;

      Comment

      • Emmash
        New Member
        • Oct 2008
        • 10

        #4
        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,$table 2,$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_tableau x_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,$table 2,$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

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          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($li ste_tableaux_co uleur, ${"table" . $count});
          $count++;
          }
          $tab_resultat_c ouleur = array_unique($l iste_tableaux_c ouleur);[/PHP]
          # 2 use the eval() function
          [PHP]$expression = '$tab_resultat_ couleur = array_merge( array_intersect (' . $liste_tableaux _couleur . ') )';
          eval( $expression );[/PHP]
          regards

          Comment

          • Emmash
            New Member
            • Oct 2008
            • 10

            #6
            I use your second proposition :

            [PHP]eval('$tab_resu ltat_couleur=ar ray_merge(array _intersect('.$l iste_tableaux_c ouleur.'))');[/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?

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              oh, I forgot the ; at the end of the string. ...stupid me
              [PHP]
              eval( '$tab_resultat_ couleur = array_merge(arr ay_intersect(' . $liste_tableaux _couleur . '));' );[/PHP]

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heya, Marie-Hélène.

                Try this, instead:

                [code=php]
                $tab_resultat_c ouleur = array_merge(cal l_user_func_arr ay('array_inter sect', $liste_tableaux _couleur));
                [/code]

                Comment

                • Emmash
                  New Member
                  • Oct 2008
                  • 10

                  #9
                  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

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by Emmash
                    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

                    Comment

                    Working...