2012-11-26 100 views
0

我正在Jira項目上工作,我從API查詢中返回一個數據。數據是json_encode格式。我有問題訪問數組內的數組。在數組內部訪問數組

這是我工作的json_encode數據。

{ 
    "expand":"names,schema", 
    "startAt":0, 
    "maxResults":50, 
    "total":1, 
    "issues":[ 
     { 
     "expand":"editmeta,renderedFields,transitions,changelog,operations", 
     "id":"190579", 
     "self":"http://jira.localhost.com/rest/api/2/issue/190579", 
     "key":"JIRA-1234", 
     "fields":{ 
      "summary":"Testing Jira ticket only", 
      "progress":{ 
       "progress":0, 
       "total":0 
      }, 
      "issuetype":{ 
       "self":"http://jira.localhost.com/rest/api/2/issuetype/28", 
       "id":"28", 
       "description":"Short task independent from any project", 
       "iconUrl":"http://jira.localhost.com/images/icons/ico_story.png", 
       "name":"Ticket", 
       "subtask":false 
      }, 
      "timespent":null, 
      "reporter":{ 
       "self":"http://jira.localhost.com/rest/api/2/user?username=username", 
       "name":"username", 
       "emailAddress":"[email protected]", 
       "avatarUrls":{ 
        "16x16":"http://jira.localhost.com/secure/useravatar?size=small&avatarId=10302", 
        "48x48":"http://jira.localhost.com/secure/useravatar?avatarId=10302" 
       }, 
       "displayName":"User Name", 
       "active":true 
      }, 
      "customfield_10430":null, 
      "customfield_10490":null, 
      "created":"2012-11-26T00:32:26.000-0800", 
      "updated":"2012-11-26T00:48:19.000-0800", 
      "description":"test description", 
      "priority":{ 
       "self":"http://jira.localhost.com/rest/api/2/priority/6", 
       "iconUrl":"http://jira.localhost.com/images/icons/priority_major.gif", 
       "name":"Normal", 
       "id":"6" 
      }, 
      "issuelinks":[ 

      ], 
      "customfield_10471":null, 
      "customfield_11054":[ 
       "server1,", 
       "server2,", 
       "server3,", 
       "server4", 
       "server5,", 
       "server6,", 
       "server7,", 
       "server8,", 
       "server9,", 
       "server10,", 
       "server11,", 
       "server12,", 
       "server13," 
      ], 
      "subtasks":[ 

      ], 
      "status":{ 
       "self":"http://jira.localhost.com/rest/api/2/status/1", 
       "description":"The issue is open and ready for the assignee to start work on it.", 
       "iconUrl":"http://jira.localhost.com/images/icons/status_open.gif", 
       "name":"Open", 
       "id":"1" 
      }, 
      "customfield_10621":null, 
      "workratio":-1, 
      "customfield_10530":{ 
       "self":"http://jira.localhost.com/rest/api/2/customFieldOption/10694", 
       "value":"S2", 
       "id":"10694" 
      }, 
      "customfield_11052":[ 
       "user.name:1", 
       "username:1" 
      ], 
      "project":{ 
       "self":"http://jira.localhost.com/rest/api/2/project/JIRA", 
       "id":"10190", 
       "key":"JIRA", 
       "name":"JIRA Tickets", 
       "avatarUrls":{ 
        "16x16":"http://jira.localhost.com/secure/projectavatar?size=small&pid=10190&avatarId=10011", 
        "48x48":"http://jira.localhost.com/secure/projectavatar?pid=10190&avatarId=10011" 
       } 
      }, 
      "customfield_10480":{ 
       "self":"http://jira.localhost.com/rest/api/2/customFieldOption/10613", 
       "value":"QT - Manual", 
       "id":"10613" 
      }, 
      "customfield_10420":null, 
      "customfield_10054":null, 
      "customfield_10053":null, 
      "customfield_10051":null, 
      "customfield_10630":null, 
      "aggregateprogress":{ 
       "progress":0, 
       "total":0 
      }, 
      "timeoriginalestimate":null, 
      "customfield_10584":null, 
      "customfield_10585":null, 
      "customfield_10582":{ 
       "self":"http://jira.localhost.com/rest/api/2/customFieldOption/10934", 
       "value":"Namesd", 
       "id":"10934" 
      }, 
      "customfield_10580":{ 
       "self":"http://jira.localhost.com/rest/api/2/customFieldOption/10808", 
       "value":"Names", 
       "id":"10808" 
      }, 
      "customfield_10581":{ 
       "self":"http://jira.localhost.com/rest/api/2/customFieldOption/10913", 
       "value":"Name", 
       "id":"10913" 
      }, 
      "votes":{ 
       "self":"http://jira.localhost.com/rest/api/2/issue/JIRA-1234/votes", 
       "votes":0, 
       "hasVoted":false 
      }, 
      "fixVersions":[ 

      ], 
      "resolution":null, 
      "resolutiondate":null, 
      "aggregatetimeoriginalestimate":null, 
      "customfield_10125":null, 
      "duedate":null, 
      "watches":{ 
       "self":"http://jira.localhost.com/rest/api/2/issue/JIRA-1234/watchers", 
       "watchCount":1, 
       "isWatching":false 
      }, 
      "customfield_10375":null, 
      "customfield_10750":"<p>testing only</p>", 
      "customfield_10651":"112965", 
      "customfield_10374":null, 
      "customfield_10650":"128836", 
      "customfield_10652":null, 
      "customfield_10370":null, 
      "assignee":{ 
       "self":"http://jira.localhost.com/rest/api/2/user?username=johnsmith", 
       "name":"johnsmith", 
       "emailAddress":"[email protected]", 
       "avatarUrls":{ 
        "16x16":"http://jira.localhost.com/secure/useravatar?size=small&avatarId=10302", 
        "48x48":"http://jira.localhost.com/secure/useravatar?avatarId=10302" 
       }, 
       "displayName":"John Smith", 
       "active":true 
      }, 
      "customfield_10590":null, 
      "customfield_10500":{ 
       "self":"http://jira.localhost.com/rest/api/2/customFieldOption/10630", 
       "value":"1", 
       "id":"10630" 
      }, 
      "aggregatetimeestimate":null, 
      "customfield_10170":null, 
      "timeestimate":null, 
      "customfield_10440":null, 
      "aggregatetimespent":null 
     } 
     } 
    ] 
} 

這是我的代碼段訪問記錄和打印。

foreach ($json_output->issues as $issues) { 

    echo "{$issues->key}"; 
    echo "{$issues->fields->summary}"; 
    echo "{$issues->fields->created}"; 
    echo "{$issues->fields->reporter->name}"; 

} 

現在,當我想訪問customfield_11054,所以我可以打印服務器的

server1, server2, server3, server4, server5, server6, server7, server8, server9, server10, 
server11, server12, server13 

的列表,所以我加入這個

foreach ($json_output->issues as $issues) { 

    echo "{$issues->key}"; 
    echo "{$issues->fields->summary}"; 
    echo "{$issues->fields->created}"; 
    echo "{$issues->fields->reporter->name}"; 
    echo "{$issues->fields->customfield_11054}";  
} 

但我得到的結果作爲數組。

+0

customfield_11054是一個數組,所以它循環:的foreach($ issues->板塊 - > customfield_11054爲$服務器)回波$服務器; – jgroenen

+0

@jgroenen是的你是對的。我試圖循環到它之前,但我得到錯誤的語法,所以它不起作用。 :) –

回答

4

print_r($issues->fields->customfield_11054);

它是一個數組,這就是爲什麼它說,它是一個數組。如果你想獲得的值,你不得不換另一種的foreach有:

 
foreach ($json_output->issues as $issues) { 

    echo $issues->key; 
    echo $issues->fields->summary; 
    echo $issues->fields->created; 
    echo $issues->fields->reporter->name; 
    foreach($issues->fields->customfield_11054 as $customField) 
    { 
     echo $customField; 
    } 
} 

此外,作爲一個側面說明,你不需要把你的變量「{}」給他們打印出來。他們是字符串,所以只需使用echo $variable;就可以工作得很好(正如我已經放入上面的snippit)。

+0

傻我。我一直在做這個。但我做的是foreach($ issues-> fields-> customfield_11054作爲$ hostname){echo「{$ hostname}」; } –

+0

謝謝你的例子。是的,它現在對我有用:)非常感謝。 –

0

使用此爲簡單起見

$data = json_decode($result,true); 

foreach ($data as $values) { 

    //then do stuff here 
} 
+0

有什麼區別?一個assoc數組與'stdClass'的一個實例在這兩種情況下,你都必須處理多個維度 –

0

implode參數iuse功能

foreach ($json_output->issues as $issues) { 

    echo "{$issues->key}"; 
    echo "{$issues->fields->summary}"; 
    echo "{$issues->fields->created}"; 
    echo "{$issues->fields->reporter->name}"; 
    echo implode(',', $issues->fields->customfield_11054);  
} 
+0

非常簡單,它的工作原理也是:) –