show array name

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

    #1

    show array name

    is there a function, that can echo/print the name of an array, so when
    i dump the array, it also says the name? For instance:

    echo array_name($mya rray);

    outputs:
    myarray

  • Mateusz Markowski

    #2
    Re: show array name


    hygum napisal(a):
    is there a function, that can echo/print the name of an array, so when
    i dump the array, it also says the name? For instance:
    >
    echo array_name($mya rray);
    >
    outputs:
    myarray
    Could you explain why do you want to do this?

    If you have written "$myarray" it means that you know the name of the
    array, so you can just write:
    echo "\$myarray"

    Comment

    • Colin Fine

      #3
      Re: show array name

      hygum wrote:
      is there a function, that can echo/print the name of an array, so when
      i dump the array, it also says the name? For instance:
      >
      echo array_name($mya rray);
      >
      outputs:
      myarray
      >
      It's hard to see how there could be.

      $myarray = array( ... );
      $otherarray = &$myarray;

      Then you want

      array_name($mya rray);

      to produce a different result from

      array_name($oth erarray);

      Do you see the problem?

      Colin

      Comment

      • Chung Leong

        #4
        Re: show array name


        hygum wrote:
        is there a function, that can echo/print the name of an array, so when
        i dump the array, it also says the name? For instance:
        >
        echo array_name($mya rray);
        >
        outputs:
        myarray
        This question pops up every now and then. The answer is you can use
        debug_backtrace () to get the source file and line number of the calling
        function, from which you can then extract the variable name with
        regexp. Not a foolproof technique and should only for debugging
        purpose.

        Do a search for debug_backtrace in this group and you should find a
        working example I posted a few years ago.

        Comment

        • hygum

          #5
          Re: show array name

          i dont know the name of the array, though i understand why you ask

          i want a function, that dumps an array nicely, to write something like
          this:

          array $myarray

          (
          0 =duck
          1 =eagle
          2 =hen
          )

          for debugging. Then i dont have to insert an echo like the one you
          suggest.

          Mateusz Markowski wrote:
          hygum napisal(a):
          is there a function, that can echo/print the name of an array, so when
          i dump the array, it also says the name? For instance:

          echo array_name($mya rray);

          outputs:
          myarray
          >
          Could you explain why do you want to do this?
          >
          If you have written "$myarray" it means that you know the name of the
          array, so you can just write:
          echo "\$myarray"

          Comment

          Working...