473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

in_array() to find an object inside an array

[PHP]
if (is_array($_POST["assoc_$key"])) {
foreach ($this->getAssocSectionsObjArray($key, $dbAP) as
$obj) {
print_r($obj); print_r(" in array? ");
print_r(in_array($obj, $result)); print_r("<P>");
if (!in_array($obj, $result)) array_push($result, $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->getAssocSectionsObjArray($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!
Jul 17 '05 #1
3 10651
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($someObjects), not
with count($someObjects) * count($objectsByOid):

$objectsInBoth = array();
while ( list($key) = each($someObjects) )
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($someArray) == serialize($anotherArray)

Phil Powell wrote:
[PHP]
if (is_array($_POST["assoc_$key"])) {
foreach ($this->getAssocSectionsObjArray($key, $dbAP) as
$obj) {
print_r($obj); print_r(" in array? ");
print_r(in_array($obj, $result)); print_r("<P>");
if (!in_array($obj, $result)) array_push($result, $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->getAssocSectionsObjArray($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!


Jul 17 '05 #2

Henk Verhoeven wrote:
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($someObjects), not
with count($someObjects) * count($objectsByOid):

$objectsInBoth = array();
while ( list($key) = each($someObjects) )
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($someArray) == serialize($anotherArray)

Phil Powell wrote:
[PHP]
if (is_array($_POST["assoc_$key"])) {
foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) {
print_r($obj); print_r(" in array? ");
print_r(in_array($obj, $result)); print_r("<P>");
if (!in_array($obj, $result)) array_push($result, $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->getAssocSectionsObjArray($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!


Jul 17 '05 #3
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($thisID, $result) { // BOOLEAN METHOD
global $section;
$id = ($this->id) ? $this->id : $_REQUEST['id'];
$associatedSection = $this->getAssociatedSection();
$obj->base_section_name = "$section";
$obj->base_section_value = $id;
$obj->associated_section_name = "$associatedSection";
$obj->associated_section_value = $thisID;
$tempResult = $result;
array_walk($tempResult, create_function('$a', 'return
serialize($a);'));
return in_array(serialize($obj), $tempResult);
}
[/PHP]

Thanx
Phil

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: point | last post by:
Hello there... I have a little issue here.... Which one is better(I think object) but would like to hear from someone more experienced.... class Persons { var $name;
4
by: Arjen | last post by:
Hi, I have a class with some attributes. For example class person with name as attribute. I have add multiple persons in an arraylist. Now I need a function to get/find a person by the name...
1
by: JeffOfJersey | last post by:
I'm trying to develop a dotnet application that uses the Word 11 com object. Whenever I try to use the find object I get a blow out. In the code fragment below executingthe ClearFormating method...
6
by: Rufnex | last post by:
Hi, is there a delete for a object inside the constructor, while i init it? i will try something like that: var obj = function(a) { if (!a) delete this; this.a = a; }
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
1
by: Katit | last post by:
Is it possible to find object in array with specific property value? Just like dataset filter? Thanks!
11
by: Bob Nelson | last post by:
It's been a long time since I've posed a query here on c.l.c. My work environment evolved to primarily C++ and Perl with very little C, so I've forgotten quite a lot over time. This revisits the...
0
by: Kim Baker | last post by:
Strange new error and I'm hoping that many others have it :-) so that the solution is easy. Until recently I could click on a DBase IV file of type DBF in explorer and Access would open and the...
2
by: AdamOnAccess | last post by:
I've recently installed Access 2007 on my windows xp system. I opened access, opened a new module, entered some code and when I attempted to save the module, a dialog box with a warning came up......
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.