douwaif22244 2017-12-08 17:00
浏览 63

如何在PHP中获取特定值的对象数组[重复]

This question already has an answer here:

I have the follow array and and am trying to get the meta_value for the the object that has the meta_key of license_number. How can I return that value?

items: [
{
    account_id: "7890",
    t: 1990007,
    meta_value: "27",
    id: "123",
    create_date: 1507174015113,
    update_date: 1512628710384,
    meta_key: "content_items",
},
{
    account_id: "7890",
    t: 1990007,
    meta_value: "123456",
    id: "123",
    create_date: 1498492590855,
    update_date: 1498492590855,
    meta_key: "location_id",
},
{
    account_id: "7890",
    t: 1990007,
    meta_value: "123456789",
    id: "123",
    create_date: 1498492590855,
    update_date: 1498492590855,
    meta_key: "license_number",
}
]
</div>
  • 写回答

1条回答 默认 最新

  • dongzhihong3940 2017-12-08 17:04
    关注

    Use array_filter() and a callable to fit your needs (here an anonymous fonction) :

    $searchedValue = 'license_number';
    $o = array_filter( $items,
        function ($e) use (&$searchedValue) {
            return $e->meta_key == $searchedValue;
        }
    );
    

    Note the return is an array of objects containing all values that fit the condition. This will give you the first result.

    reset($o); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类