如何获得对象在数组中的位置?

时间:2017-12-20 10:13:46

标签: php

我的阵列:

$array = array(
    'test' => new Object(2),
    'two' => new Object(22),
    'other' => new Object(12),
    'five' => new Object(23),
    'next' => new Object(42),
);

我如何在这个数组中获得位置?

我有变量:

$object = Object(12);

所以我想收到:

$position = 3; //or 2 if we number from 0

array_keys在此示例中不起作用...

1 个答案:

答案 0 :(得分:0)

您可以使用$position = array_search($object, array_values($array), true) + 1;

mkdir in batch file as adminarray_search manual

使用array_values删除密钥并获取索引数组。因为您需要位置而不是元素的索引,您需要为其添加1。

array_values manual

相关问题