2017-07-13 23 views
0

調用其他JSON我需要你的help..I想使用JSON值來調用一個JSON值...JSON使用PHP

這裏是我的樣品JSON ...

第一JSON:

{ 
    "first_name": "Richard Catibog", 
    "last_name": "", 
    "display_name": "Richard Catibog ", 
    "roles": { 
    "owner": false, 
    "administrator": true 
    }, 
    "login_count": 108, 
    "enabled": true, 
    "departments": [ 
    1817719575, 
    1817719887, 
    1817722991 
    ], 
    "id": 4142095, 
    "last_login": "2017-07-12T10:03:57Z", 
    "create_date": "2017-04-28T12:09:57Z", 
    "email": "[email protected]" 
}, 
{ 
    "first_name": "Rovi Cruz", 
    "last_name": "", 
    "display_name": "Rovi Roy Cruz ", 
    "roles": { 
    "owner": false, 
    "administrator": true 
    }, 
    "login_count": 98, 
    "enabled": true, 
    "departments": [ 
    1817719575, 
    1817719887, 
    1817722991 
    ], 
    "id": 4225009, 
    "last_login": "2017-07-13T00:37:27Z", 
    "create_date": "2017-06-05T06:15:49Z", 
    "email": "[email protected]" 
} 

二JSON:

[ 
    { 
    "description": "Fees Support for American Academy", 
    "settings": { 

    }, 
    "enabled": false, 
    "id": 1817719575, 
    "name": "AA Fees Support [L2]" 
    }, 
    { 
    "description": "American Academy", 
    "settings": { 

    }, 
    "enabled": true, 
    "id": 1817722991, 
    "name": "Lesson and Fee Support" 
    }, 
    { 
    "description": "Technical Support", 
    "settings": { 

    }, 
    "enabled": true, 
    "id": 1817719887, 
    "name": "Technical Support" 
    } 
] 

我想呼籲日的ID Ë第二和迴音使用在第一JSON部門ID列表中的名稱...

這裏就是我得到... enter image description here 但我希望看到的結果是AA費用支持[L2],課程和費用支持和技術支持...

謝謝你,希望你能幫助我...

+0

你願意提供和示例結果,所以我們可以清楚地看到你的意思 – RiggsFolly

+0

@RiggsFolly完成編輯 – ivor

回答

1

在此我提出,是在departm鍵入一個數組$desc已廢除的ID號碼,以便您可以將ID很容易地轉換爲Descrition

$js1 = '{ 
     "first_name": "Richard Catibog", "last_name": "", 
     "display_name": "Richard Catibog ", 
     "roles": { "owner": false,"administrator": true}, 
     "login_count": 108,"enabled": true, 
     "departments": [1817719575,1817719887,1817722991], 
     "id": 4142095,"last_login": "2017-07-12T10:03:57Z", 
     "create_date": "2017-04-28T12:09:57Z", 
     "email": "[email protected]" 
}'; 

$js2 = '[ 
    { 
    "description": "Fees Support for American Academy", 
    "settings": {},"enabled": false,"id": 1817719575, 
    "name": "AA Fees Support [L2]" 
    }, 
    { 
    "description": "American Academy", 
    "settings": {},"enabled": true,"id": 1817722991, 
    "name": "Lesson and Fee Support" 
    }, 
    { 
    "description": "Technical Support", 
    "settings": {},"enabled": true,"id": 1817719887, 
    "name": "Technical Support" 
    } 
]'; 

$first = json_decode($js1); 
//print_r($first); 
$second = json_decode($js2); 
//print_r($second); 

// make array key'd on the id 
$desc = []; 
foreach ($second as $s) { 
    $desc[$s->id] = $s; 
} 
print_r($desc); 

// go through all the dept id's in the first array 
// getting a matching desctipyion from array 2 
foreach ($first->departments as $dept) { 
    echo $desc[$dept]->description . PHP_EOL; 
} 

UPDATE:第一JSON數據結構後得到了改變對象的數組

注意我改變了你的第一個JSON數據結構來使其有效JSON,方法是在2個對象周圍添加[]以生成有效的JSON數據結構,即對象數組。

在這種情況下,你需要做的就是添加一個循環來覆蓋現在的數組對象。

$js1 = '[{ 
    "first_name": "Richard Catibog", 
    "last_name": "", 
    "display_name": "Richard Catibog ", 
    "roles": { "owner": false, "administrator": true }, 
    "login_count": 108, 
    "enabled": true, 
    "departments": [ 1817719575, 1817719887, 1817722991 ], 
    "id": 4142095, 
    "last_login": "2017-07-12T10:03:57Z", 
    "create_date": "2017-04-28T12:09:57Z", 
    "email": "[email protected]" 
}, 
{ 
    "first_name": "Rovi Cruz", 
    "last_name": "", 
    "display_name": "Rovi Roy Cruz ", 
    "roles": { "owner": false, "administrator": true }, 
    "login_count": 98, 
    "enabled": true, 
    "departments": [ 1817719575, 1817719887, 1817722991 ], 
    "id": 4225009, 
    "last_login": "2017-07-13T00:37:27Z", 
    "create_date": "2017-06-05T06:15:49Z", 
    "email": "[email protected]" 
}]'; 

$js2 = '[ 
    { 
    "description": "Fees Support for American Academy", 
    "settings": {},"enabled": false,"id": 1817719575, 
    "name": "AA Fees Support [L2]" 
    }, 
    { 
    "description": "American Academy", 
    "settings": {},"enabled": true,"id": 1817722991, 
    "name": "Lesson and Fee Support" 
    }, 
    { 
    "description": "Technical Support", 
    "settings": {},"enabled": true,"id": 1817719887, 
    "name": "Technical Support" 
    } 
]'; 

$first = json_decode($js1); 
//print_r($first); 
$second = json_decode($js2); 
//print_r($second); 

// make array key'd on the id 
$desc = []; 
foreach ($second as $s) { 
    $desc[$s->id] = $s; 
} 
//print_r($desc); 

//Loop through all the first array of object 
foreach ($first as $f) { 
    // go through all the dept id's in the first array 
    // getting a matching desctipyion from array 2 
    foreach ($f->departments as $dept) { 
     echo $desc[$dept]->description . PHP_EOL; 
    } 
    echo PHP_EOL; 
} 

結果

Fees Support for American Academy 
Technical Support 
American Academy 

Fees Support for American Academy 
Technical Support 
American Academy 
+0

也出現錯誤'試圖獲得非對象的屬性在' – ivor

+0

必須是在實現中的東西,它運行在這裏就好了 – RiggsFolly

+0

注意我不使用轉換爲數組選項的json_decode。它是一個非常好的對象,所以我這樣離開 – RiggsFolly

0

在這裏你去

$json1 = '{ 
     "first_name": "Richard Catibog", 
     "last_name": "", 
     "display_name": "Richard Catibog ", 
     "roles": { 
      "owner": false, 
      "administrator": true 
     }, 
     "login_count": 108, 
     "enabled": true, 
     "departments": [ 
      1817719575, 
      1817719887, 
      1817722991 
     ], 
     "id": 4142095, 
     "last_login": "2017-07-12T10:03:57Z", 
     "create_date": "2017-04-28T12:09:57Z", 
     "email": "[email protected]" 
} 
'; 
$json2 ='[ 
    { 
    "description": "Fees Support for American Academy", 
    "settings": { 

    }, 
    "enabled": false, 
    "id": 1817719575, 
    "name": "AA Fees Support [L2]" 
    }, 
    { 
    "description": "American Academy", 
    "settings": { 

    }, 
    "enabled": true, 
    "id": 1817722991, 
    "name": "Lesson and Fee Support" 
    }, 
    { 
    "description": "Technical Support", 
    "settings": { 

    }, 
    "enabled": true, 
    "id": 1817719887, 
    "name": "Technical Support" 
    } 
]'; 

$json1 = json_decode($json1, true); 
$json2 = json_decode($json2, true); 

$department = function($id) use($json2) { 
    foreach($json2 as $dep) { 
     if($dep["id"] == $id) { 
      return $dep; 
     } 
    } 
    return false; 
}; 

foreach($json1["departments"] as $id) { 
    $dep = $department($id); 
    if(is_array($dep)) { 
     echo sprintf("id: %s, name: %s\n", $id, $dep["name"]); 
    } 

} 
+0

我得到了一個錯誤...未定義指數:部門 – ivor

+0

即時猜測你的第一個json是一個對象數組,而不僅僅是一個對象,正如你在你的問題中提到的那樣?在第一個循環中放置另一個foreach循環,並循環訪問json1的元素。 –