Re: in_array() related problem

From: Date: Sun, 05 Nov 2006 13:51:51 +0000
Subject: Re: in_array() related problem
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-244236@lists.php.net to get a copy of this message
Hi, Thanks for your reply. After a sleep overnight I found I said something really stupid. Arrays are compared in deep, and also for objects. I really forgot the old PHP4 way and thought PHP5 compares object simply by address when using ==, which is not the real case. I need to use === for comparing objects of the same instance. And thanks Tom for pointing out to use the strict parameter. On 11/4/06, Richard Lynch <ceo@l-i-e.com> wrote:
Try providing a custom comparison function. Almost for sure, PHP is attempting to "test" the == by a deeper scan than you think. On Fri, November 3, 2006 10:56 am, tamcy wrote:
Hello all, I'm new to this list. To not flooding the bug tracking system I hope to clarify some of my understanding here. I am referring to the (now bogus) bug report http://bugs.php.net/bug.php?id=39356&edit=2. This happens after my upgrade to PHP 5.2, where the code shown produces a "Fatal error: Nesting level too deep - recursive dependency?". Same testing code reproduced below: ---------------------------- <?php class A {
      public $b;
} class B {
      public $a;
} $a = new A; $b = new B; $b->a = $a; $a->b = $b; $test = array($a, $b); var_dump(in_array($a, $test)); ---------------------------- I think this is not rare for a child item to have knowledge about its parent, forming a cross-reference. This code runs with no problem in PHP5.1.6, but not in 5.2. Ilia kindly points out that "In php 5 objects are passed by reference, so your code does in fact create a circular dependency.". I know the passed by reference rule. What I'm now puzzled is, why this should lead to an error. To my knowledge, despite the type-casting issue and actual algorithm, in_array() should actually do nothing more than: function mimic_in_array($search, $list) { foreach ($list as $item)
    if ($search == $item)
      return true;
return false; } Which means: 1. in_array() isn't multi-dimensional. 2. in_array() doesn't care about the properties of any object. That is, I don't expect in_array() to nest through all available inner arrays for a match, not to mention those are object properties, not arrays. So here is the question: Why should in_array() throws such a "Fatal error: Nesting level too deep" error? Why should it care? Is there any behaviour I don't know? Thanks all in advance. Tamcy -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So?


« previous php.general (#244236) next »