简体   繁体   English

如何找到多维数组中最后出现的项的键?

[英]How can I find the key of the last occurrence of an item in a multidimensional array?

I need to find the key that corresponds to the last occurrence of (id = 100001203541047) in the following array: 我需要在以下数组中找到与(id = 100001203541047)的最后一次出现相对应的键:

[0] => Array
        (
            [id] => 10152583762905798_10152583800415798
            [from] => Array
                (
                    [id] => 100001203541047
                    [name] => Gangareddy Chealimealla
                )

            [message] => Desi Flipkart
            [created_time] => 2014-07-30T07:30:34+0000
            [like_count] => 0
            [user_likes] =>
            [can_comment] => 1
        )

    [1] => Array
        (
            [id] => 10152583762905798_10152583786375798
            [from] => Array
                (
                    [id] => 100001430479186
                    [name] => Pratik Das
                )

            [message] => flipkart rules! (y)
            [created_time] => 2014-07-30T07:16:56+0000
            [like_count] => 0
            [user_likes] =>
            [can_comment] => 1
        )

    [2] => Array
        (

            [id] => 10152583762905798_10152583802415798
            [from] => Array
                (
                    [id] => 100001203541047
                    [name] => Gangareddy Chealimealla
                )

            [message] => Desi Flipkart
            [created_time] => 2014-07-30T08:30:34+0000
            [like_count] => 0
            [user_likes] =>
            [can_comment] => 1
        )

I tried the following code, but it's returning 2 positions. 我尝试了以下代码,但它返回2个位置。

 foreach($arr as $key => $array)
 {
     if ( $array['from']['id'] === $id)
         echo $key."\n\n";
     }
 }

Why doesn't this only show the last one? 为什么这不只显示最后一个?

To do it simple, you could store them in an array and after the loop, take the last one. 为了简单起见,您可以将它们存储在一个数组中,然后在循环之后执行最后一个。

change 更改

echo $key."\n\n";

to

$ids[] = $key

And after the loop, you'll have your last occurence in 循环之后,您将最后一次出现在

end($ids);

Hope it help you. 希望对您有帮助。

// function where you can pass the id you are looking for, eg.: 100001203541047.
function findElementById($id, $arr) {
    // iterate from the back, and you get the last match.
    for ($i = count($arr) - 1; $i >= 0; $i--) {
        $array = $arr[$i];
        if($array['from']['id'] === $id)){
            return $array; // if you find the element return it.
        }
    }
    return null; // return null if not found.
}

Example usage: 用法示例:

$elementYouAreLookingFor = findElementById(100001203541047, $arr);

Assuming the array is already sorted, you can use array_map to find the ones containing your search variable and then simply use array_pop to get the last result. 假设数组已经排序,则可以使用array_map查找包含搜索变量的数组,然后只需使用array_pop即可获取最后的结果。

For example: this function only returns the last array value with the magic ID. 例如:此函数仅返回带有魔术ID的最后一个数组值。

$magicID = '100001203541047';
function findSame($value){
    if($value['from']['id'] == $magicID){
        return $value;
    }
}

// $values should contain your data
$values = array(array(1,'from'=>array('id'=>'100001203541047'),3), array(1,'from'=>array('id'=>'20000'),4), array(2,'from'=>array('id'=>'100001203541047'),3));
$mapping = array_pop(array_filter(array_map('findSame', $values)));
var_dump($mapping);

Here's a fun one (PHP >= 5.5.0): 这是一个有趣的示例(PHP> = 5.5.0):

echo array_search($id,
     array_reverse(array_column(array_column($array, 'from'), 'id'), true));

The code you already wrote almost works. 您已经编写的代码几乎可以正常工作。 The problem is that it will just immediately print the key each time it encounters one that matches the id during the foreach loop. 问题在于,每次在foreach循环中遇到与ID匹配的密钥时,它将立即立即打印密钥。

foreach($arr as $key => $array) {
    if ( $array['from']['id'] === $id) {
        echo $key."\n\n";
    }
}

If instead you store the key in a variable, then after the loop is finished, the value in the variable will be the last one, and you can just print that. 相反,如果将密钥存储在变量中,则在循环结束后,变量中的值将是最后一个,您可以直接打印出来。

$last = "";
foreach($arr as $key => $array) {
    if ( $array['from']['id'] === $id) {
        $last = $key;
    }
}
echo $last."\n\n";

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

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