How to find index of object in php array?

前端 未结 10 1898
时光说笑
时光说笑 2021-02-13 18:46

Here is print_r output of my array:

Array
(
[0] => stdClass Object
    (
        [itemId] => 560639000019
        [name] => Item no1
        [code] =>         


        
10条回答
  •  醉酒成梦
    2021-02-13 19:32

    Currently php does not have any supported function for this yet.

    So refer to Java's Vector, or jQuery's $.inArray(), it would simply be:

    public function indexOf($object, array $elementData) {
        $elementCount = count($elementData);
        for ($i = 0 ; $i < $elementCount ; $i++){
            if ($object == $elementData[$i]) {
                return $i;   
            }
        }
        return -1;
    }
    

    You can save this function as a core function for later.

提交回复
热议问题