Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3948382
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:22:04+00:00 2026-05-20T01:22:04+00:00

In a PHP program I have an array of some custom objects, and I

  • 0

In a PHP program I have an array of some custom objects, and I want to find if the array contains a certain object. Of course I can use array_search, but this checks if the objects are the same object, not if it has the same variables. So I want to be able to create my own compare function for the objects, which I can use with the array_search method (or something similar).
I want to be able to do something like this:

class foo
{
    public $_a,$_b;
    function __construct($a,$b)
    {
        $this->_a = $a;
        $this->_b = $b;
    }

    function __equals($object)
    {
        return $this->_a == $object->_a;
    }
}
$f1 = new foo(5,4);
$f2 = new foo(4,6);
$f3 = new foo(4,5);

$array = array($f1,$f2);
$idx = array_search($f3,$array); // return 0

Is something like this possible?
I know I can also create my own array_search method which uses a method from the class, but than I’d have to use 2 different search functions, one for the classes which do have their own compare function, and one for those which haven’t.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:22:05+00:00Added an answer on May 20, 2026 at 1:22 am

    Here’s a neat little trick I recently found out:

    class Foo {
        public $a;
        public $b;
    
        public function __toString() {
            return (string)$this->a;
        }
    
        public function __construct($a, $b) {
             $this->a = $a;
             $this->b = $b;
        }
    
    }
    
    $a = new Foo(1, 'a');
    $b = new Foo(2, 'b');
    $c = new Foo(3, 'c');
    $d = new Foo(2, 'd');
    $array = array($a, $b);
    
    $key = array_search($d, $array);         // false
    
    $key = array_search((string)$c, $array); // false
    $key = array_search((string)$d, $array); // 1
    

    This also works:

    $is_equal = ((string)$d == $b);          // true
    

    When passed a string $needle, array_search will try to cast the objects contained in $haystack to string to compare them, by calling the __toString magic method if it exists, which in this case returns Foo::$a.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script that runs as a CGI program and the HTTP
I've got a PHP command line program running. And I want to connect to
I'm working on a program that uses PHP's internal array pointers to iterate along
I'm writing server-side programs in PHP for an iPhone app. And I have no
I have a LAMP (PHP) web app which need to interface with programs on
I have several independent executable Perl, PHP CLI scripts and C++ programs for which
Alright, quick question. A server is running in Eastern Time. PHP program needs to
Hi im learning LISP and well, all day i program php for a living,
I'm planning to make a very simple program using php and mySQL. The main
PHP has a great function called htmlspecialcharacters() where you pass it a string and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.