如何在php中按对象数组中的属性搜索? [英] How to search by a property in array of objects in php?

查看:484
本文介绍了如何在php中按对象数组中的属性搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,如下所示 如果我在php中提供了平板ID值,是否可以找到数组的键索引?

i have an array something like given below is it possible to find the key index of the array if i provide a slab id value in php?

Array
       (
           [0] => incentiveSlab Object
               (
                   [slabId] => 1
                   [templateId] => 1
                   [startPoint] => 0
                   [endPoint] => 1000000
                   [value] => 0
               )

           [1] => incentiveSlab Object
               (
                   [slabId] => 2
                   [templateId] => 1
                   [startPoint] => 1000000
                   [endPoint] => 2500000
                   [value] => 0.5
               )


       )

推荐答案

我会建议更改您的数据结构

I will suggest, change your Data Structure

  • 使用slabId作为Array_Index
  • 例如

    • use slabId as Array_Index
    • Eg.

      数组

      (

         [0] => NULL
      
         [1] => incentiveSlab Object
             (
                 [slabId] => 1
                 [templateId] => 1
                 [startPoint] => 0
                 [endPoint] => 1000000
                 [value] => 0
             )
      
         [2] => incentiveSlab Object
             (
                 [slabId] => 2
                 [templateId] => 1
                 [startPoint] => 1000000
                 [endPoint] => 2500000
                 [value] => 0.5
             )
      

      )

      或者如果slabId中的方差太大,请使用关联数组

      Or if you have too much variance in slabId use Associative Array

      这篇关于如何在php中按对象数组中的属性搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆