2
$className = "Class B: Wednesday 6pm";
// $studentArray is multidimensional array with student info

foreach($studentArray as $student) {
    echo array_search($className,$student);
}

// Contents of $student is 
Array
(
    [Groups] => 187,267
    [Birthday] => DateTime Object
        (
            [date] => 1981-02-04 00:00:00.000000
            [timezone_type] => 3
            [timezone] => UTC
        )

    [_IGTestScore] => 0
    [Email] => [email protected]
    [_IGClass1] => Class B: Wednesday 6pm
    [_IGAttendedClass1] => DateTime Object
        (
            [date] => 2016-02-17 00:00:00.000000
            [timezone_type] => 3
            [timezone] => UTC
        )

    [FirstName] => Joe
    [Id] => 3
    [LastName] => Schmoe
)

The output of that is:

_IGTestScore

If I do a var_dump on $className and $student['_IGClass1] I get:

string(22) "Class B: Wednesday 6pm"

string(22) "Class B: Wednesday 6pm"

Never had an issue with array_search before, but this has been driving me crazy, and can't seem to figure out what is going on here. It seems that any string search is coming out the same. But if I do a search for an integer, like 3, it will pull out "Id" correctly.

UPDATE - whole multi-dimensional array

Array
(
    [0] => Array
        (
            [Groups] => 187,267
            [Birthday] => DateTime Object
                (
                    [date] => 1981-02-04 00:00:00.000000
                    [timezone_type] => 3
                    [timezone] => UTC
                )

            [_IGTestScore] => 0
            [Email] => [email protected]
            [_IGClass1] => Class B: Wednesday 6pm
            [_IGAttendedClass1] => DateTime Object
                (
                    [date] => 2016-02-17 00:00:00.000000
                    [timezone_type] => 3
                    [timezone] => UTC
                )

            [FirstName] => Joe
            [Id] => 3
            [LastName] => Schmoe
        )

)
9
  • Your Class B: Wednesday 6pm value in the array may have a space in the end, like this: Class B: Wednesday 6pm . And this might be the issue. Commented Feb 22, 2016 at 22:48
  • @Nordenheim - I showed the var_dump to show that they are both identical. Commented Feb 22, 2016 at 22:50
  • It works correctly for me (tested). Sure that one of the sub-arrays is not malformed? You have same result for each sub-array? Commented Feb 22, 2016 at 22:55
  • @fusion3k - I can change the needle to other string values (like Joe, [email protected]) but the result is the same. Any string element (FirstName, Email, etc.) will return as "_IGTestScore". I can access the elements fine individually, such as $student['_IGClass1'] and the value returns fine. Commented Feb 22, 2016 at 23:00
  • Also you have to var_dump($student['_IGTestScore']), not _IGClass1... ッ Commented Feb 22, 2016 at 23:00

1 Answer 1

2

I have found the problem... but I don't know the reason: for me, it is a mystery!

The problem is _IGTestScore as integer: if _IGTestScore is an integer, array_search() returns _IGTestScore ( In fact it returns _IGTestScore for any searchstring, even if they doesn't exists! ), if _IGTestScore is set to '0' (string) or to a positive integer, array_search() returns correct value!

It's a bug?

Someone have an explanation?

Edit:

Obviously, there is a solution: use the “strict” parameter:

array_search( $className, $student, True );

But... Why “Class B: Wednesday 6pm” == 0?

Sign up to request clarification or add additional context in comments.

1 Comment

Strict worked for me, but still puzzled as why it would behave this way regardless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.