繁体   English   中英

如何在 PHP 多维数组中找到索引 position 匹配键

[英]How to find the index position matched key in PHP multidimensional array

我有一个多维数组。 我想在该数组中找到给定键的 position。

我的阵列

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
        )

)

我正在传递UPID值,并且我需要该 UPID 的索引 position。

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

我预期的 output 是 3,因为 300 索引 position 是 3

一切都是正确的,但不是array_values使用array_keys

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM