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 8798941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:12:18+00:00 2026-06-14T00:12:18+00:00

I recently noticed trouble when using the array_search function in my code. I am

  • 0

I recently noticed trouble when using the array_search function in my code. I am searching the array “$allcraftatts” for the value “sharp”. I tried to isolate the problem by setting up a two line experiment:

$testcopy=$allcraftatts;
$testsharp=array_search("sharp", $testcopy);

Using “print_r(get_defined_vars());” later on, I get this result:

[testcopy] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                    [6] => Sharp Stone
                    [7] => Sharp Stones
                    [8] => stone
                    [9] => object
                    [10] => sharp
                    [11] => hard
                    [12] => 0
                    [13] => 0
                    [14] => 0
                    [15] => 0
                    [16] => 0
                    [17] => 0
                    [18] => 0
                )

[testsharp] => 0

I made sure that I do not modify these variables at any other time.

Now, if I change my code to

$testcopy=$allcraftatts;
unset($testcopy[0]);
$testsharp=array_search("sharp", $testcopy);

it returns “1”.

This leads me to believe that it always returns the first key in the array.

It baffles me! This is one of those bugs that makes you fear something wrong with the language itself. However doubtful this is, I actually was eventually driven to looking at the PHP source for something wrong there, but unfortunately could not understand it.

Seeing that it is a function simple as this, I will most definitely be completely humiliated by the inevitably simple answer, but at this point, I just want an answer.

  • 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-06-14T00:12:20+00:00Added an answer on June 14, 2026 at 12:12 am

    array_search is using == to compare values during search

    FORM PHP DOC

    If the third parameter strict is set to TRUE then the array_search() function will search for identical elements in the haystack. This means it will also check the types of the needle in the haystack, and objects must be the same instance.

    Becasue the first element is 0 the string was converted to 0 during search

    Simple Test

    var_dump("sharp" == 0); //true
    var_dump("sharp" === 0); //false
    

    Solution use strict option to search identical values

    $testsharp = array_search("sharp", $testcopy,true);
                                                   ^---- Strict Option
    
    var_dump($testsharp);
    

    Output

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

Sidebar

Related Questions

I'm having trouble setting up multicast transport using ActiveMQ. I noticed with version 5.2
I've been editing CSS using Firebug in Firefox, but recently noticed that Chrome is
I recently noticed some strange behavior when using a returned empty shared_ptr. To illustrate
At my company we recently noticed that one developer was not using language files
I have recently noticed that my test code is too close to the production
I have been using Rstudio a great deal these days but recently noticed that
I recently noticed the following code which basically defines a class method public Func<string,
I recently noticed lots of stores using the same URLS I cant seem to
I have a vb app and i recently noticed a strange problem with Groupboxes
Hi I have recently noticed that when Running the function geoCoder.getFromLocationName() on android 2.2

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.