从Json Array PHP中提取此字段

时间:2018-06-11 15:27:55

标签: php json

大家好,我一直在使用通过api返回的JSON数组。

[ 
{ "Id": "81654bdf-f56d-4e0e-ad3a-57860ad5be24", 
"Name": "TESCO PLC", 
"Address": { "StreetNumber": null, 
         "StreetName": "TESCO HOUSE SHIRE PARK", 
         "PostCode": "AL7 1GA", 
         "City": "WELWYN GARDEN CITY", 
         "StateCode": "AL", 
         "CountryCode": "GB" }, 
"PhoneNumber": "44-1992632222", 
"LegalFormCode": "PLC", 
"ExternalIds": [ { "ExternalId": "1057587", "ExternalService": "eulerid" },
             { "ExternalId": "00445790", "ExternalService": "CRO" }, 
             { "ExternalId": "216854067", "ExternalService": "DUN" }, 
             { "ExternalId": "2189357501", "ExternalService": "HRM" }, 
             { "ExternalId": "220430231", "ExternalService": "TVAGB" }, 
             { "ExternalId": "345370", "ExternalService": "ACI" }, 
             { "ExternalId": "376445", "ExternalService": "HCIS" }, 
             { "ExternalId": "38", "ExternalService": "GROUP" }, 
             { "ExternalId": "432577", "ExternalService": "PNCH" }, 
             { "ExternalId": "5594389", "ExternalService": "SIACID" }, 
             { "ExternalId": "574064", "ExternalService": "EHIPNR" }, 
             { "ExternalId": "7013000615", "ExternalService": "EURODB" }, 
             { "ExternalId": "FE0181198", "ExternalService": "SFACID" }, 
             { "ExternalId": "G14788", "ExternalService": "KMV" } ] },

提取所使用的街道名称:    $ buyerStreetName = $行['地址'] [' StreetName'];

我现在如何尝试捕获eulerid并且不知道如何执行此操作,因为它没有唯一的字段名称所以我需要将ExternalId放在ExternalService = eulerid

的位置

感谢您的帮助

2 个答案:

答案 0 :(得分:6)

这应该是诀窍:

private void init(){
    Log.d(TAG, "init: initialising");

    mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
            if(actionId == EditorInfo.IME_ACTION_SEARCH
                    || actionId == EditorInfo.IME_ACTION_DONE
                    || keyEvent.getAction() == KeyEvent.ACTION_DOWN
                    || keyEvent.getAction() == keyEvent.KEYCODE_ENTER) {

                //execute method for searching
                geoLocate();


            }
            return false;
        }
    });

}

答案 1 :(得分:2)

我倾向于使用array_column()来创建数据的关联数组,以便于访问,因此代码归结为...

foreach ( $data as $row )   {
    $externalIds = $row["ExternalIds"];
    $externalIds = array_column($externalIds, "ExternalId", "ExternalService");
    echo $externalIds["eulerid"];
}

(外部foreach()可能是你已经拥有的东西,只是为了完整性)