returning Integer array to PHP from C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    returning Integer array to PHP from C

    Hi... I have got a problem.

    I try to return a integer array from C tp PHP but unable to get the values back..I am using SWIG to integrate C and PHP.

    My .c looks like.

    Code:
    int arr[2];
    int *arr_ret()
     {
         arr[0]=2;
         arr[1]=4;
         return arr;
     }
    My .i looks like:

    Code:
    %module example
     %{
    extern int arr[2];
    %}
     extern int arr[2];

    My php is:

    Code:
    <?php
    
    include("example.php");
    
    $t = array(get_time());
    
    print_r $t;
    
    var_dump($t);
    ?>
    ----------------------------------------------------------------


    How to retrive the integer array in PHP?

    Regards
    Dheeraj Joshi
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Your PHP doesn't call the arr_ret() method.

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      Sorry mark...... That was some other function.

      My PHP looks like,

      Code:
      
      <?php
      include("example.php");
      $t = array(arr_ret());
      print_r $t;
      var_dump($t);
      ?>
      Regards
      Dheeraj Joshi

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        I've no idea - you might better asking on a SWIG mailing list / forum.

        Mark.

        P.S. Have you tried writing a PHP (zend) extension to wrap the C functions? It's pretty easy.

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          I read introduction to ZEND..

          Dont know why.. But it scared me.

          Regards
          Dheeraj Joshi

          Comment

          • Dheeraj Joshi
            Recognized Expert Top Contributor
            • Jul 2009
            • 1129

            #6
            I am getting this output in PHP.

            #Resource id #5

            Regards
            Dheeraj Joshi

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              C array in PHP via SWIG

              Hi... Does anybody know, how to manipulate the C arrays in PHP via SWIG?

              Since in C

              Code:
              return arr;
              return the address not the value. How this can be handled in PHP?

              I read SWIG documentation.. But not much helpful.

              Regards
              Dheeraj Joshi

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                I have merged your threads because they are largely on the same topic.

                Like I said previously, you'll have more success if you ask a question on the mailing lists.

                Mark.

                Comment

                • Dheeraj Joshi
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1129

                  #9
                  Thanks for merging them...

                  I already mailed to mailing list...

                  Mail archieve is not getting updated from 21st Aug.. So i dont get their mails.. I dont know why...

                  And we are running short of time.. Need some work arounds..

                  Regards
                  Dheeraj Joshi

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    The mailing lists seem to have had a lot of use this month.

                    Comment

                    • hsriat
                      Recognized Expert Top Contributor
                      • Jan 2008
                      • 1653

                      #11
                      Do not use pointers. That will return address only, not value.
                      Try this: (not sure if it will work)
                      Code:
                      int arr_ret()
                       {
                           int arr[2];
                           arr[0]=2;
                           arr[1]=4;
                           return arr;
                       }
                      I you can't change C code, change php like:
                      Code:
                      $arr = "";
                      &$arr = arr_ret();
                      print_r($arr);

                      Comment

                      • Dheeraj Joshi
                        Recognized Expert Top Contributor
                        • Jul 2009
                        • 1129

                        #12
                        Without taking pointers it works fine..

                        But C code belong to some other organization and i dont have permission to change it..

                        But i did a work around for that problem..

                        Now i am able to call C functions in PHP.. HURRAY....

                        Regards
                        Dheeraj Joshi

                        Comment

                        • nsharishbabu
                          New Member
                          • Aug 2011
                          • 1

                          #13
                          can you please give me the solution for the above mentioned problem

                          Comment

                          • Dheeraj Joshi
                            Recognized Expert Top Contributor
                            • Jul 2009
                            • 1129

                            #14
                            Use SWIG to wrap c data structures and methods as PHP variables and methods.

                            Regards
                            Dheeraj Joshi

                            Comment

                            Working...