0

I have a multidimensional array. i want to find the position of given key in that array.

my array

Array
(
    [200] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 2
        )

    [100] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 3
        )

    [400] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 4
        )

    [300] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 7
        )

    [500] => Array
        (
            [totalQuesAttempted] => 10
            [totalCorrectQuestion] => 8
        )

)

i am passing UPID value, and i need index position of that UPID.

$UPID = "300";
$result = array_search($UPID, array_values($usersAttemptsInfo)); 

my expected output is 3 because 300 index position is 3

0

1 Answer 1

1

Everything is correct but instead of array_values use array_keys:

$UPID = "300";
$result = array_search($UPID, array_keys($usersAttemptsInfo)); 
echo $result;
Sign up to request clarification or add additional context in comments.

Comments

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.