dpgu5074 2012-09-18 00:05
浏览 44
已采纳

在数组中缓存json数据并引用id来填充类型

My function returns a bunch of attribute ids and it's corresponding names in json format. For eg:

{
    "DETAILS": [
        {
            "ATTR_ID": "522",
            "ATTRIBUTE_ID": "4222",
            "ATTRIBUTE_TYPE": "email",

        },
        {
            "ATTR_ID": "523",
            "ATTRIBUTE_ID": "4006",
            "ATTRIBUTE_TYPE": "interest",


        }
    ]
}

I need to store it in an array which i need to cache .( I'll be probably using $_SESSION)

Now another function returns me set of attribute ids:

 $val_array = $subarray[arrayOfAttributes];
    foreach ($val_array as $val)
    {
        print "Attrib Value: $val
"; //This will print ids 4222,4223,etc
    }
what I need to do is correspond the ids with the type from the cache nad print out the respective Types.

I am unable to formulate this logic into code.

  • 写回答

1条回答 默认 最新

  • dongtun2459 2012-09-18 02:10
    关注
    function getAttrType($id, $array){
      // $array is your 'DETAILS' array.
      foreach($array as $a){
        if($a['ATTR_ID'] == $id){
          return $a['ATTRIBUTE_TYPE'];
        }
      }
    }
    
    print getAttrType(523, $array); // will output 'interest'
    print getAttrType(522, $array); // will output 'email'
    print getAttrType(123, $array); // will output nothing
    

    No idea if this is what you're looking for, maybe this can get you in the right direction.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度