Problem with array of objects

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

    #1

    Problem with array of objects

    Hi! I have a small problem with array and objects:

    $a = new A();
    $b = new A();
    $c = new A();
    $d = new A();
    $e = new A();

    $ar = array($a, $c, $d);

    There is a method inside A, which would need the information, that is
    that instance ($this) of A a member of array $ar.

    I thought, that this could have been tested easily with

    if (in_array($this , $ar)) ...

    But it is not working.

    Is there any easy solution for this?

    Thanks,

    H.
  • David Rybach

    #2
    Re: Problem with array of objects

    Hannu Tiitu wrote:[color=blue]
    > Hi! I have a small problem with array and objects:
    >
    > $a = new A();
    > $b = new A();
    > $c = new A();
    > $d = new A();
    > $e = new A();
    >
    > $ar = array($a, $c, $d);
    >
    > There is a method inside A, which would need the information, that is
    > that instance ($this) of A a member of array $ar.
    >
    > I thought, that this could have been tested easily with
    >
    > if (in_array($this , $ar)) ...
    >
    > But it is not working.
    >
    > Is there any easy solution for this?[/color]

    If the instances of the class differ in their attributes it would be a way
    to write a function like that:

    function obj_in_array($n eedle, $haystack) {
    foreach($haysta ck as $v) {
    if($v === $needle) return true;
    }
    return false;
    }

    For more information about comparison of objects see:
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    Regards,
    David

    Comment

    • Hannu Tiitu

      #3
      Re: Problem with array of objects

      David Rybach <david@rybach.d e> wrote...

      Thanks a lot for the info! This was very helpful for me! H.

      Comment

      • Shawn Wilson

        #4
        Re: Problem with array of objects

        Hannu Tiitu wrote:[color=blue]
        >
        > Hi! I have a small problem with array and objects:
        >
        > $a = new A();
        > $b = new A();
        > $c = new A();
        > $d = new A();
        > $e = new A();
        >
        > $ar = array($a, $c, $d);
        >
        > There is a method inside A, which would need the information, that is
        > that instance ($this) of A a member of array $ar.
        >
        > I thought, that this could have been tested easily with
        >
        > if (in_array($this , $ar)) ...
        >
        > But it is not working.
        >
        > Is there any easy solution for this?[/color]

        Is $ar a global? And is it declared as a global in your method of A?

        Regards,
        Shawn
        --
        Shawn Wilson
        shawn@glassgian t.com


        I have a spam filter. Please include "PHP" in the
        subject line to ensure I'll get your message.

        Comment

        Working...