How to tell php a variable is an array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sueyic@gmail.com

    How to tell php a variable is an array?

    Hello,

    What is the syntax for telling php that a variable is an array, or an
    array of arrays?

    Let's say I have $arrTerms

    /*
    @param array of arrays $arrTerms
    */
    function test($arrTerms) {
    $anotherArray = array('1', '2');

    $intersect = array_intersect ($anotherArray, $arrTerms[0]);

    }

    This doesn't work and php tells me I'm trying to access $arrTerms[0] as
    a array though it is not.

    But actually, since $arrTerms is supposed to be an array of arrays --
    $arrTerms[0] is an array ... how can I tell php this?

    Thanks,
    sue yi

  • Chung Leong

    #2
    Re: How to tell php a variable is an array?

    sueyic@gmail.co m wrote:[color=blue]
    > Hello,
    >
    > What is the syntax for telling php that a variable is an array, or an
    > array of arrays?
    >
    > Let's say I have $arrTerms
    >
    > /*
    > @param array of arrays $arrTerms
    > */
    > function test($arrTerms) {
    > $anotherArray = array('1', '2');
    >
    > $intersect = array_intersect ($anotherArray, $arrTerms[0]);
    >
    > }
    >
    > This doesn't work and php tells me I'm trying to access $arrTerms[0] as
    > a array though it is not.
    >
    > But actually, since $arrTerms is supposed to be an array of arrays --
    > $arrTerms[0] is an array ... how can I tell php this?[/color]

    To tell if a variable holds an array--use is_array(). To force a
    variable into being an array, do (array) $var or settype($var,
    'array').

    Comment

    • sueyic@gmail.com

      #3
      Re: How to tell php a variable is an array?

      Hi,

      This is exactly the sort of answer I wanted ... thank you!! =)

      Comment

      • Carl Vondrick

        #4
        Re: How to tell php a variable is an array?

        Chung Leong wrote:[color=blue][color=green]
        >> What is the syntax for telling php that a variable is an array, or an
        >> array of arrays?[/color][/color]

        You can also type hint it:

        function test(array $arrTerms) {

        This requires PHP 5.x, however.

        --
        Carl Vondrick
        Web-Engineer
        Professor of Computer Science at Columbia University, researching computer vision, machine learning, and AI applications.

        Comment

        • Mladen Gogala

          #5
          Re: How to tell php a variable is an array?

          On Thu, 13 Apr 2006 15:20:10 -0700, Chung Leong wrote:
          [color=blue]
          > To tell if a variable holds an array--use is_array(). To force a
          > variable into being an array, do (array) $var or settype($var,
          > 'array').[/color]

          Of course, type casting prevents you from seeing mistakes. I prefer to
          kill the script if the scalar is passed instead of array. For that, there
          is an "exit" function: if (!is_array($var )) {
          exit(" var should be an array at line:".__LINE__ );
          }


          --


          Comment

          Working...