dsaf415212 2011-03-09 01:32
浏览 231
已采纳

从json数据中提取唯一键名,例如SQL select与**********不同

I have the following json data:

{
   "data": [
      {
         "name": "The Frugalicious Chef",
         "category": "Chef",
         "id": "186397894735983",
         "created_time": "2011-03-07T16:10:35+0000"
      },
      {
         "name": "Siuslaw Broadband",
         "category": "Telecommunication",
         "id": "190373850988171",
         "created_time": "2011-03-06T20:21:42+0000"
      },
      {
         "name": "Paul",
         "category": "Movie",
         "id": "129989595478",
         "created_time": "2011-03-04T19:55:18+0000"
      },
      {
         "name": "Mark Zuckerberg",
         "category": "Public figure",
         "id": "68310606562",
         "created_time": "2011-02-16T09:50:35+0000"
      },

The idea here is that I want to take this data and use parts of it. I want to create a list of the "category's" that are in the data. The problem is that there is and will be multiple items with the same category. So my list will have duplicates that I do not want. The following is how I am getting the data and converting it for use:

$jsonurl = "https://xxxxxxxxxx.com/".$fd_ID. "/info?access_token=".$session['access_token'];
$likesjson = file_get_contents($jsonurl,0,null,null);
$likesArray=json_decode($likesjson);

I then use a foreach to access the data.

foreach($friendLikesArray->data as $l)
{
etc......
}

So I guess muy question is I want to take the $likesArray and pull out all the unique Data->Category->names. Also will want to do sorting, and other things but I will get to that when the time comes.

Thanks for the help in advance. Neil

  • 写回答

1条回答 默认 最新

  • duanshan188866 2011-03-09 01:38
    关注

    The data structure you would want to use is a set, that only allows unique entries.

    A simple implementation using PHP arrays is to use the keys.

    e.g.

    $categories = array();
    
    foreach($friendLikesArray->data as $l)
    {
        $categories[$l->category] = true;
    }
    
    $categories = array_keys($categories);
    

    This way if the category has already been added, then you are not adding anything new to the array.

    If the keys are not important to you then you can use the line:

    $categories[$l->category] = $l->category
    

    But this means your array won't have 0,1,2...n for keys.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题