in_array() to find an object inside an array

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

    in_array() to find an object inside an array

    [PHP]
    if (is_array($_POS T["assoc_$key "])) {
    foreach ($this->getAssocSectio nsObjArray($key , $dbAP) as
    $obj) {
    print_r($obj); print_r(" in array? ");
    print_r(in_arra y($obj, $result)); print_r("<P>");
    if (!in_array($obj , $result)) array_push($res ult, $obj);
    }
    }
    [/PHP]

    Here is the output of the result of this script:

    stdClass Object ( [id] => 2 [placement_name] => Placement #1 ) in
    array?
    Warning: in_array(): Wrong datatype for first argument in
    /image_catalog/include/classes.inc.php on line 99


    Warning: in_array(): Wrong datatype for first argument in
    /www/html/mu-spin/image_catalog/include/classes.inc.php on line 100
    stdClass Object ( [id] => 4 [placement_name] => Placement #10 ) in
    array?
    Warning: in_array(): Wrong datatype for first argument in
    /image_catalog/include/classes.inc.php on line 99


    Warning: in_array(): Wrong datatype for first argument in
    /www/html/mu-spin/image_catalog/include/classes.inc.php on line 100
    stdClass Object ( [id] => 6 [placement_name] =>
    http://blah.com ) in array?
    Warning: in_array(): Wrong datatype for first argument in
    /image_catalog/include/classes.inc.php on line 99


    Warning: in_array(): Wrong datatype for first argument in
    /image_catalog/include/classes.inc.php on line 100
    What I have to do is compare an array of objects, $result, with
    another array of objects, $this->getAssocSectio nsObjArray($key ,
    $dbAP). Both arrays will be identical in structure and object
    formatting, but not in object data content. What I need to do is to
    find out if that one array has the very same object as the other, then
    don't add it to $result, otherwise, add to $result.

    The object formatting is as follows:

    obj: stdClass Object ( [id] => 2 [placement_name] => Placement
    #1 )
    And $result can look like this:

    result: Array ( [0] => stdClass Object ( [id] => 6 [placement_name] =>
    Placement #27 ) [1] => stdClass Object ( [id] => 2 [placement_name] =>
    Placement #1 ) )
    I don't wish to loop through both arrays, that would be a performance
    nightmare to do that. Is there a version of in_array in existence for
    comparing objects?

    Thanx
    Phil

    PS: I went to this manual example at

    but that involves recursive handling and object rebuilding - this,
    were I to figure out what the living blue it is, would have to be put
    inside another loop.. OUCH!
  • Henk Verhoeven

    #2
    Re: in_array() to find an object inside an array

    Hi Phil,

    why give each object a unique oid? Then you can do:

    $objectsByOid[SanObject->getOid()] =& $anObject;

    Then you can see if an object is in $objectsByOid by:

    isSet( $objectsByOid[SsomeObject->getOid()] )

    BTW, i think in_array() does do a sequential search, so it wont be so
    much faster then those while loops. The above uses associative keys,
    which is probably implemented with hashing. Proper hashing is about half
    as fast as indexed lookup, but it does not substantially slow down for
    large arrays.

    So the following should perform linear with count($someObje cts), not
    with count($someObje cts) * count($objectsB yOid):

    $objectsInBoth = array();
    while ( list($key) = each($someObjec ts) )
    if (isSet( $objectsByOid[SsomeObjects[$key]->getOid()] )
    $objectsInBoth[] =& SsomeObjects[$key];

    Greetings,

    Henk Verhoeven,
    www.phpPeanuts.org.

    BTW, to compare two arrays at once, you may use
    $someArray == $anotherArray, unless they contain objects or arrays with
    circular references, in that case you neet to resort to:
    serialize($some Array) == serialize($anot herArray)

    Phil Powell wrote:[color=blue]
    > [PHP]
    > if (is_array($_POS T["assoc_$key "])) {
    > foreach ($this->getAssocSectio nsObjArray($key , $dbAP) as
    > $obj) {
    > print_r($obj); print_r(" in array? ");
    > print_r(in_arra y($obj, $result)); print_r("<P>");
    > if (!in_array($obj , $result)) array_push($res ult, $obj);
    > }
    > }
    > [/PHP]
    >
    > Here is the output of the result of this script:
    >
    >
    > stdClass Object ( [id] => 2 [placement_name] => Placement #1 ) in
    > array?
    > Warning: in_array(): Wrong datatype for first argument in
    > /image_catalog/include/classes.inc.php on line 99
    >
    >
    > Warning: in_array(): Wrong datatype for first argument in
    > /www/html/mu-spin/image_catalog/include/classes.inc.php on line 100
    > stdClass Object ( [id] => 4 [placement_name] => Placement #10 ) in
    > array?
    > Warning: in_array(): Wrong datatype for first argument in
    > /image_catalog/include/classes.inc.php on line 99
    >
    >
    > Warning: in_array(): Wrong datatype for first argument in
    > /www/html/mu-spin/image_catalog/include/classes.inc.php on line 100
    > stdClass Object ( [id] => 6 [placement_name] =>
    > http://blah.com ) in array?
    > Warning: in_array(): Wrong datatype for first argument in
    > /image_catalog/include/classes.inc.php on line 99
    >
    >
    > Warning: in_array(): Wrong datatype for first argument in
    > /image_catalog/include/classes.inc.php on line 100
    >
    >
    > What I have to do is compare an array of objects, $result, with
    > another array of objects, $this->getAssocSectio nsObjArray($key ,
    > $dbAP). Both arrays will be identical in structure and object
    > formatting, but not in object data content. What I need to do is to
    > find out if that one array has the very same object as the other, then
    > don't add it to $result, otherwise, add to $result.
    >
    > The object formatting is as follows:
    >
    >
    obj: stdClass Object ( [id] => 2 [placement_name] => Placement
    > #1 )
    >
    > And $result can look like this:
    >
    >
    > result: Array ( [0] => stdClass Object ( [id] => 6 [placement_name] =>
    > Placement #27 ) [1] => stdClass Object ( [id] => 2 [placement_name] =>
    > Placement #1 ) )
    >
    > I don't wish to loop through both arrays, that would be a performance
    > nightmare to do that. Is there a version of in_array in existence for
    > comparing objects?
    >
    > Thanx
    > Phil
    >
    > PS: I went to this manual example at
    > http://us3.php.net/manual/en/functio...rray.php#40079
    > but that involves recursive handling and object rebuilding - this,
    > were I to figure out what the living blue it is, would have to be put
    > inside another loop.. OUCH![/color]

    Comment

    • phillip.s.powell@gmail.com

      #3
      Re: in_array() to find an object inside an array


      Henk Verhoeven wrote:[color=blue]
      > Hi Phil,
      >
      > why give each object a unique oid? Then you can do:
      >
      > $objectsByOid[SanObject->getOid()] =& $anObject;
      >
      > Then you can see if an object is in $objectsByOid by:
      >
      > isSet( $objectsByOid[SsomeObject->getOid()] )
      >
      > BTW, i think in_array() does do a sequential search, so it wont be so[/color]
      [color=blue]
      > much faster then those while loops. The above uses associative keys,
      > which is probably implemented with hashing. Proper hashing is about[/color]
      half[color=blue]
      > as fast as indexed lookup, but it does not substantially slow down[/color]
      for[color=blue]
      > large arrays.
      >
      > So the following should perform linear with count($someObje cts), not
      > with count($someObje cts) * count($objectsB yOid):
      >
      > $objectsInBoth = array();
      > while ( list($key) = each($someObjec ts) )
      > if (isSet( $objectsByOid[SsomeObjects[$key]->getOid()] )
      > $objectsInBoth[] =& SsomeObjects[$key];
      >
      > Greetings,
      >
      > Henk Verhoeven,
      > www.phpPeanuts.org.
      >
      > BTW, to compare two arrays at once, you may use
      > $someArray == $anotherArray, unless they contain objects or arrays[/color]
      with[color=blue]
      > circular references, in that case you neet to resort to:
      > serialize($some Array) == serialize($anot herArray)
      >
      > Phil Powell wrote:[color=green]
      > > [PHP]
      > > if (is_array($_POS T["assoc_$key "])) {
      > > foreach ($this->getAssocSectio nsObjArray($key , $dbAP)[/color][/color]
      as[color=blue][color=green]
      > > $obj) {
      > > print_r($obj); print_r(" in array? ");
      > > print_r(in_arra y($obj, $result)); print_r("<P>");
      > > if (!in_array($obj , $result)) array_push($res ult,[/color][/color]
      $obj);[color=blue][color=green]
      > > }
      > > }
      > > [/PHP]
      > >
      > > Here is the output of the result of this script:
      > >
      > >
      > > stdClass Object ( [id] => 2 [placement_name] => Placement #1 ) in
      > > array?
      > > Warning: in_array(): Wrong datatype for first argument in
      > > /image_catalog/include/classes.inc.php on line 99
      > >
      > >
      > > Warning: in_array(): Wrong datatype for first argument in
      > > /www/html/mu-spin/image_catalog/include/classes.inc.php on line 100
      > > stdClass Object ( [id] => 4 [placement_name] => Placement #10 ) in
      > > array?
      > > Warning: in_array(): Wrong datatype for first argument in
      > > /image_catalog/include/classes.inc.php on line 99
      > >
      > >
      > > Warning: in_array(): Wrong datatype for first argument in
      > > /www/html/mu-spin/image_catalog/include/classes.inc.php on line 100
      > > stdClass Object ( [id] => 6 [placement_name] =>
      > > http://blah.com ) in array?
      > > Warning: in_array(): Wrong datatype for first argument in
      > > /image_catalog/include/classes.inc.php on line 99
      > >
      > >
      > > Warning: in_array(): Wrong datatype for first argument in
      > > /image_catalog/include/classes.inc.php on line 100
      > >
      > >
      > > What I have to do is compare an array of objects, $result, with
      > > another array of objects, $this->getAssocSectio nsObjArray($key ,
      > > $dbAP). Both arrays will be identical in structure and object
      > > formatting, but not in object data content. What I need to do is[/color][/color]
      to[color=blue][color=green]
      > > find out if that one array has the very same object as the other,[/color][/color]
      then[color=blue][color=green]
      > > don't add it to $result, otherwise, add to $result.
      > >
      > > The object formatting is as follows:
      > >
      > >
      obj: stdClass Object ( [id] => 2 [placement_name] =>[/color][/color]
      Placement[color=blue][color=green]
      > > #1 )
      > >
      > > And $result can look like this:
      > >
      > >
      > > result: Array ( [0] => stdClass Object ( [id] => 6 [placement_name][/color][/color]
      =>[color=blue][color=green]
      > > Placement #27 ) [1] => stdClass Object ( [id] => 2 [placement_name][/color][/color]
      =>[color=blue][color=green]
      > > Placement #1 ) )
      > >
      > > I don't wish to loop through both arrays, that would be a[/color][/color]
      performance[color=blue][color=green]
      > > nightmare to do that. Is there a version of in_array in existence[/color][/color]
      for[color=blue][color=green]
      > > comparing objects?
      > >
      > > Thanx
      > > Phil
      > >
      > > PS: I went to this manual example at
      > > http://us3.php.net/manual/en/functio...rray.php#40079
      > > but that involves recursive handling and object rebuilding - this,
      > > were I to figure out what the living blue it is, would have to be[/color][/color]
      put[color=blue][color=green]
      > > inside another loop.. OUCH![/color][/color]

      Comment

      • phillip.s.powell@gmail.com

        #4
        Re: in_array() to find an object inside an array

        Thanx but I'm sorry, I can't follow your reply, it honestly doesn't
        make sense to me.

        Can you explain it a bit more simply to me?

        Thanx
        Phil

        PS: Never mind, I got it!

        [PHP]
        /**
        * Resultset version of "in_array" function to display boolean results
        of finding a resultset-configured object in the resultset
        *
        * @access private
        * @param int $thisID
        * @param object $result
        * @return boolean
        * @see http://us2.php.net/manual/en/function.in-array.php
        */
        function in_result($this ID, $result) { // BOOLEAN METHOD
        global $section;
        $id = ($this->id) ? $this->id : $_REQUEST['id'];
        $associatedSect ion = $this->getAssociatedS ection();
        $obj->base_section_n ame = "$section";
        $obj->base_section_v alue = $id;
        $obj->associated_sec tion_name = "$associatedSec tion";
        $obj->associated_sec tion_value = $thisID;
        $tempResult = $result;
        array_walk($tem pResult, create_function ('$a', 'return
        serialize($a);' ));
        return in_array(serial ize($obj), $tempResult);
        }
        [/PHP]

        Thanx
        Phil

        Comment

        Working...