Returning a 2 dimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Parrot

    #1

    Returning a 2 dimensional array

    I am trying to program a function to return a 2 dimensional array, but
    it's not working. I reduced the return value to 1 dimension and
    tested that to make sure that the problem wasn't elsewhere. It had no
    problem when the array was 1 dimensional.

    Is there something different I need to do in order to return a 2
    dimensional array from a function?
  • JDS

    #2
    Re: Returning a 2 dimensional array

    On Thu, 03 Nov 2005 12:10:55 -0500, Parrot wrote:
    [color=blue]
    > Is there something different I need to do in order to return a 2
    > dimensional array from a function?[/color]

    Yes. Create a 2-D array inside the function. Return it.

    As simple as this:

    function get_2d_array(){
    return array(array());
    }

    Maybe your function is not *creating* a 2D array?
    --
    JDS | jeffrey@example .invalid
    | http://www.newtnotes.com
    DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

    Comment

    • Parrot

      #3
      Re: Returning a 2 dimensional array

      I definitely created a 2D array, I tested it within the function.

      Your example doesn't give an example of creating a 2D array and I
      don't see any variable names in your return statement. Could you
      please expand on that?

      On Thu, 03 Nov 2005 14:48:19 -0500, JDS <jeffrey@exampl e.invalid>
      wrote:
      [color=blue]
      >On Thu, 03 Nov 2005 12:10:55 -0500, Parrot wrote:
      >[color=green]
      >> Is there something different I need to do in order to return a 2
      >> dimensional array from a function?[/color]
      >
      >Yes. Create a 2-D array inside the function. Return it.
      >
      >As simple as this:
      >
      >function get_2d_array(){
      > return array(array());
      >}
      >
      >Maybe your function is not *creating* a 2D array?[/color]

      Comment

      • Malcolm Dew-Jones

        #4
        Re: Returning a 2 dimensional array

        Parrot (me@myplace.com ) TOP POSTED:
        : I definitely created a 2D array, I tested it within the function.

        Let's see the code.

        : Your example doesn't give an example of creating a 2D array and I

        It looks like an array of arrays to me. Perhaps you mean something else
        when you say "2D array".

        : don't see any variable names in your return statement. Could you
        : please expand on that?

        What does the name of the variable have to do with this? He returns a php
        "thing" which is an array of arrays. You could have assigned that "thing"
        to a variable first if you wished, and then returned the value of the
        thing by using the variable. I will admit I can not test that at the
        moment, but find it hard to imagine it doesn't work as I expect...

        $two_D_array = array(array());
        return $two_D_array;

        We need to see your code to debug the error or misunderstandin g.


        : On Thu, 03 Nov 2005 14:48:19 -0500, JDS <jeffrey@exampl e.invalid>
        : wrote:

        : >On Thu, 03 Nov 2005 12:10:55 -0500, Parrot wrote:
        : >
        : >> Is there something different I need to do in order to return a 2
        : >> dimensional array from a function?
        : >
        : >Yes. Create a 2-D array inside the function. Return it.
        : >
        : >As simple as this:
        : >
        : >function get_2d_array(){
        : > return array(array());
        : >}
        : >
        : >Maybe your function is not *creating* a 2D array?


        --

        This programmer available for rent.

        Comment

        • Parrot

          #5
          Re: Returning a 2 dimensional array

          On 3 Nov 2005 12:57:34 -0700, yf110@vtn1.vict oria.tc.ca (Malcolm
          Dew-Jones) wrote:
          [color=blue]
          >
          >We need to see your code to debug the error or misunderstandin g.
          >[/color]

          Okay:


          function maincats()
          {
          global $datHost;
          global $datUser;
          global $datPass;
          global $datName;

          $db1 = mysql_connect($ datHost,$datUse r,$datPass) or die
          ("Unable to connect: ". mysql_error());
          mysql_select_db ($datName);

          $qstr="select * from categories where parent_id='0'";
          $qry=mysql_quer y($qstr) or die ("Unable to set query:
          $qstr<BR>". mysql_error());
          $count = 0;
          while($arr=mysq l_fetch_array($ qry))
          {
          $cat[$count][0] = $arr['cat_id'];
          $cat[$count][1] = $arr['name'];
          $count += 1;
          }
          return $cat;
          }

          Comment

          • Chung Leong

            #6
            Re: Returning a 2 dimensional array

            Don't see anything wrong with the code. I suspect the problem lies in
            the way you access the array. Do a print_r() on the return value and
            see if you're getting what you're supposed to.

            Comment

            • Parrot

              #7
              Re: Returning a 2 dimensional array

              On 3 Nov 2005 14:28:49 -0800, "Chung Leong"
              <chernyshevsky@ hotmail.com> wrote:
              [color=blue]
              >Don't see anything wrong with the code. I suspect the problem lies in
              >the way you access the array. Do a print_r() on the return value and
              >see if you're getting what you're supposed to.[/color]

              I set up a simple test script to try and figure out how to work the
              arrays:

              <?
              function test()
              {
              $array[0][0] = "Hello";
              $array[0][1] = "World";
              $array[1][0] = "How Are";
              $array[1][1] = "You?";

              return array($array);
              }

              $array = test();
              echo $array[0][0];
              ?>

              When I run it it just prints out the word "Array".

              When I replace the "echo $array[0][0];" line with "print_r ($array);"
              it gives me:

              Array ( [0] => Array ( [0] => Array ( [0] => Hello [1] => World ) [1]
              => Array ( [0] => How Are [1] => You? ) ) )

              Is it returning a 3 dimensional array or something??

              Comment

              • Richard Brooks

                #8
                Re: Returning a 2 dimensional array

                Parrot wrote:[color=blue]
                > On 3 Nov 2005 14:28:49 -0800, "Chung Leong"
                > <chernyshevsky@ hotmail.com> wrote:
                >
                >[color=green]
                >>Don't see anything wrong with the code. I suspect the problem lies in
                >>the way you access the array. Do a print_r() on the return value and
                >>see if you're getting what you're supposed to.[/color]
                >
                >
                > I set up a simple test script to try and figure out how to work the
                > arrays:
                >
                > <?
                > function test()
                > {
                > $array[0][0] = "Hello";
                > $array[0][1] = "World";
                > $array[1][0] = "How Are";
                > $array[1][1] = "You?";
                >
                > return array($array);
                > }
                >
                > $array = test();
                > echo $array[0][0];
                > ?>
                >
                > When I run it it just prints out the word "Array".
                >
                > When I replace the "echo $array[0][0];" line with "print_r ($array);"
                > it gives me:
                >
                > Array ( [0] => Array ( [0] => Array ( [0] => Hello [1] => World ) [1]
                > => Array ( [0] => How Are [1] => You? ) ) )
                >
                > Is it returning a 3 dimensional array or something??[/color]


                I tend to go this site a lot!

                <http://uk.php.net/types.array>

                Richard.

                Comment

                • Chung Leong

                  #9
                  Re: Returning a 2 dimensional array

                  Ah, you got confused by JDS's cryptic message. To return an array, you
                  just return it. A call to array() would create a new array with
                  whatever passed as its elements. For example, the following would give
                  you an array with 2 elements, "Hello" and "World".

                  $array = array("Hello", "World");

                  The following is what you need to create a array of arrays:

                  function test()
                  {
                  $array[0][0] = "Hello";
                  $array[0][1] = "World";
                  $array[1][0] = "How Are";
                  $array[1][1] = "You?";
                  return $array;
                  }

                  I said "array of arrays" instead of "2-D array" because that's what
                  they are. Strictly speaking, the function doesn't create one 2-D array;
                  it creates three arrays--two inside the third. We could've created the
                  same structure with the following:

                  function test()
                  {
                  $array = array(
                  array("Hello", "World"),
                  array("How are", "You?")
                  );
                  return $array;
                  }

                  or

                  function test()
                  {
                  $array = array();
                  $array[] = array("Hello", "World");
                  $array[] = array("How are", "You?");
                  return $array;
                  }

                  Personally I prefer the last syntax, as it's more obvious what's
                  happening: (1) an empty array is created (2) a new array with two
                  strings is added (3) another array with two strings is added.

                  Now going back to your code, you could have coded the while loop like
                  this

                  while($arr=mysq l_fetch_array($ qry))
                  {
                  $cat[] = array($arr['cat_id'], $arr['name']);
                  }

                  Comment

                  • Richard Brooks

                    #10
                    Re: Returning a 2 dimensional array

                    Parrot wrote:[color=blue]
                    > On 3 Nov 2005 14:28:49 -0800, "Chung Leong">
                    > <chernyshevsky@ hotmail.com> wrote:
                    >
                    >[color=green]
                    >>Don't see anything wrong with the code. I suspect the problem lies in
                    >>the way you access the array. Do a print_r() on the return value and
                    >>see if you're getting what you're supposed to.[/color]
                    >
                    >
                    > I set up a simple test script to try and figure out how to work the
                    > arrays:
                    >
                    > <?
                    > function test()
                    > {
                    > $array[0][0] = "Hello";
                    > $array[0][1] = "World";
                    > $array[1][0] = "How Are";
                    > $array[1][1] = "You?";
                    >
                    > return array($array);
                    > }
                    >
                    > $array = test();
                    > echo $array[0][0];
                    > ?>
                    >
                    > When I run it it just prints out the word "Array".
                    >[/color]

                    Doing a Google search I came across this URL but the interesting thing
                    was the last line.

                    "Multi-dimensional arrays

                    PHP does not explicitly support multi-dimensional arrays. However PHP
                    allows arrays of any type of object, including arrays so two dimensional
                    arrays can be implemented as arrays of arrays. "

                    <http://www.scit.wlv.ac .uk/~jphb/sst/php/lang/phparrays.html>


                    Richard.

                    Comment

                    • Parrot

                      #11
                      Re: Returning a 2 dimensional array

                      [color=blue]
                      >The following is what you need to create a array of arrays:
                      >
                      >function test()
                      >{
                      > $array[0][0] = "Hello";
                      > $array[0][1] = "World";
                      > $array[1][0] = "How Are";
                      > $array[1][1] = "You?";
                      > return $array;
                      >}
                      >[/color]

                      Yeah, that's the one I tried, but it didn't work.

                      [color=blue]
                      >function test()
                      >{
                      > $array = array();
                      > $array[] = array("Hello", "World");
                      > $array[] = array("How are", "You?");
                      > return $array;
                      >}[/color]

                      This one did work. But now I'm wondering why it did and the first one
                      didn't?

                      Comment

                      • Chung Leong

                        #12
                        Re: Returning a 2 dimensional array


                        Parrot wrote:[color=blue][color=green]
                        > >The following is what you need to create a array of arrays:
                        > >
                        > >function test()
                        > >{
                        > > $array[0][0] = "Hello";
                        > > $array[0][1] = "World";
                        > > $array[1][0] = "How Are";
                        > > $array[1][1] = "You?";
                        > > return $array;
                        > >}
                        > >[/color]
                        >
                        > Yeah, that's the one I tried, but it didn't work.[/color]

                        What do you mean it didn't work? What did you get when you do a
                        print_r()? The output should be:

                        Array
                        (
                        [0] => Array
                        (
                        [0] => Hello
                        [1] => World
                        )

                        [1] => Array
                        (
                        [0] => How Are
                        [1] => You?
                        )

                        )

                        If you're not getting that then you version of PHP is buggy.

                        Comment

                        • Parrot

                          #13
                          Re: Returning a 2 dimensional array

                          >What do you mean it didn't work? What did you get when you do a[color=blue]
                          >print_r()? The output should be:
                          >
                          >Array
                          >(
                          > [0] => Array
                          > (
                          > [0] => Hello
                          > [1] => World
                          > )
                          >
                          > [1] => Array
                          > (
                          > [0] => How Are
                          > [1] => You?
                          > )
                          >
                          >)
                          >
                          >If you're not getting that then you version of PHP is buggy.[/color]

                          I already told you what I got, it was:

                          Array
                          (
                          [0] => Array
                          (
                          [0] => Array
                          (
                          [0] => Hello
                          [1] => World
                          )
                          [1] => Array
                          (
                          [0] => How Are
                          [1] => You?
                          )
                          )
                          )

                          Are you sure it's PHP that's buggy? I don't want to be the handiman
                          who blames his tools when something goes wrong.

                          Comment

                          • Chung Leong

                            #14
                            Re: Returning a 2 dimensional array


                            Parrot wrote:[color=blue]
                            > I already told you what I got, it was:
                            >
                            > Array
                            > (
                            > [0] => Array
                            > (
                            > [0] => Array
                            > (
                            > [0] => Hello
                            > [1] => World
                            > )
                            > [1] => Array
                            > (
                            > [0] => How Are
                            > [1] => You?
                            > )
                            > )
                            > )
                            >
                            > Are you sure it's PHP that's buggy? I don't want to be the handiman
                            > who blames his tools when something goes wrong.[/color]

                            That's the result from the following:

                            1: function test()
                            2:{
                            3: $array[0][0] = "Hello";
                            4: $array[0][1] = "World";
                            5: $array[1][0] = "How Are";
                            6: $array[1][1] = "You?";
                            7: return array($array);
                            8:}

                            The function I gave you was:

                            1: function test()
                            2:{
                            3: $array[0][0] = "Hello";
                            4: $array[0][1] = "World";
                            5: $array[1][0] = "How Are";
                            6: $array[1][1] = "You?";
                            7: return $array;
                            8:}

                            Note the difference at line 7.

                            Comment

                            • JDS

                              #15
                              Re: Returning a 2 dimensional array

                              On Thu, 03 Nov 2005 14:58:58 -0500, Parrot wrote:
                              [color=blue]
                              > Your example doesn't give an example of creating a 2D array and I don't
                              > see any variable names in your return statement. Could you please expand
                              > on that?[/color]

                              Yes, it does. A 2D array is just an array inside an array. I just
                              created an empty example.

                              $twodary = array(array());

                              There it is. The simplest 2D array.

                              Look at it like this:

                              $twodary[0] == array()

                              and

                              $twodary[0][0] == null

                              That is 2 dimensions.
                              --
                              JDS | jeffrey@example .invalid
                              | http://www.newtnotes.com
                              DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

                              Comment

                              Working...