Jump to content

will array_search handle objects?


sangoku

Recommended Posts

Hy I was just wondering, on php site there are words in this function that it searches objects to...

 

i have a array in which many instances of a same object are stored... i need only the object with the right attribute in it. is the a possibility to work here with array_search?

 

i was plying around but ... nothing.. if someone else can make it work... congratulation!

 

<?php

   

class ab{

    private static $count = 0;

    public $a = 'aaa';

    public $k = 'aa';

    protected $b = 'ba';

    private $c='ca';

    function ab(){

        ab::$count++;

        $this->a = ab::$count.'a';

        $this->b = ab::$count.'b';

        $this->c = ab::$count.'c';

    }

}

$a = array();

while($ba < 10){

    $a[]= new ab();

    $ba++;

}

 

$b = array_search('1a',$a);

echo $b;

?>

 

I mean to make it work so it uses arraz search not some made up functions... i can do that alone... already did...

Link to comment
https://forums.phpfreaks.com/topic/200702-will-array_search-handle-objects/
Share on other sites

How do you expect it to find an object, if you ask it to look for a string? ('1a').

 

To find an object, you must look for object.

 

See this code:

 

<?php

class A {}
class B {}
class C {}

$a = new A();
$b = new B();
$c = new C();

$arr = array($a,$b,$c);

var_dump(array_search(new C(), $arr));   // > int(2)  - it finds index of an object of same class
var_dump(array_search(new C(), $arr, true));  // bool(false)  - strict mode, looks for exactly same instance of the object
var_dump(array_search($c, $arr, true));  // int(2)  

WO.oW

 

Ok that is useful... sometimes... but i cant use that. because i have  an array which has many instances of objects... but here is the tricky part i dont know all attributes of the class...  only one... so it is not the exact instance.... any way around that?

 

Great one mate thank ya(damn that  goblin way of speak won`t  go out of ma heed  >:(  the one from halls of stone  from WOW....  :'( )

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.