在此数组中找到“id”的正确路径

时间:2016-05-12 17:46:59

标签: php arrays

我正在使用Mavenlink API创建项目:

$response = $client->createWorkspace(array("title" => $project_number . ' ' . $company_name, "creator_role" => "maven"));

print_r($response);

$project_id = $response->id;

echo "project id: " . $project_id;

创建项目的部分工作正常,但我找不到“id”的正确路径。我想要ID,然后我可以使用更新方法向项目添加更多信息。

下面是响应输出的截图:

enter image description here

原始回复:

{"count":1,"workspaces":{"12220207":{"title":"34343 Add New","archived":false,"description":"","due_date":"","effective_due_date":"","start_date":"","budgeted":true,"change_orders_enabled":true,"updated_at":"2016-05-12T10:51:01-07:00","created_at":"2016-05-12T10:51:01-07:00","consultant_role_name":"FUEL","client_role_name":"Clients","percentage_complete":0,"access_level":"open","exclude_archived_stories_percent_complete":false,"can_create_line_items":true,"default_rate":"125.00","currency_symbol":"$","currency_base_unit":100,"can_invite":true,"has_budget_access":true,"tasks_default_non_billable":false,"rate_card_id":null,"workspace_invoice_preference_id":null,"posts_require_privacy_decision":false,"require_time_approvals":false,"require_expense_approvals":false,"stories_are_fixed_fee_by_default":false,"price":"TBD","price_in_cents":null,"budget_used":"$0","over_budget":false,"currency":"USD","expenses_in_burn_rate":true,"budget_used_in_cents":0,"total_expenses_in_cents":0,"status":{"color":"grey","message":"Not Started","key":130},"permissions":{"can_upload_files":true,"can_private_message":true,"can_join":false,"is_participant":true,"access_level":"admin","team_lead":true,"user_is_client":false},"creator_id":"10340340030","id":"12220207"}},"results":[{"key":"workspaces","id":"12220207"}]}

当我回显project_id时,它显示为空白。定位“id”的正确途径是什么?

谢谢!

3 个答案:

答案 0 :(得分:0)

只需json解码,你就会得到它。假设你有$json你的json。

Online check

$arr = json_decode($json);    
echo $arr->workspaces->{12220207}->id; //12220207

获取结果id

echo $arr->results[0]->id; //12220207

答案 1 :(得分:0)

$json = '{"count":1,"workspaces":{"12220207":{"title":"34343 Add New","archived":false,"description":"","due_date":"","effective_due_date":"","start_date":"","budgeted":true,"change_orders_enabled":true,"updated_at":"2016-05-12T10:51:01-07:00","created_at":"2016-05-12T10:51:01-07:00","consultant_role_name":"FUEL","client_role_name":"Clients","percentage_complete":0,"access_level":"open","exclude_archived_stories_percent_complete":false,"can_create_line_items":true,"default_rate":"125.00","currency_symbol":"$","currency_base_unit":100,"can_invite":true,"has_budget_access":true,"tasks_default_non_billable":false,"rate_card_id":null,"workspace_invoice_preference_id":null,"posts_require_privacy_decision":false,"require_time_approvals":false,"require_expense_approvals":false,"stories_are_fixed_fee_by_default":false,"price":"TBD","price_in_cents":null,"budget_used":"$0","over_budget":false,"currency":"USD","expenses_in_burn_rate":true,"budget_used_in_cents":0,"total_expenses_in_cents":0,"status":{"color":"grey","message":"Not Started","key":130},"permissions":{"can_upload_files":true,"can_private_message":true,"can_join":false,"is_participant":true,"access_level":"admin","team_lead":true,"user_is_client":false},"creator_id":"6333747","id":"12220207"}},"results":[{"key":"workspaces","id":"12220207"}]}';
$result = json_decode($json,true);    
//print_r($result);
echo $result['results'][0]['id'];

http://sandbox.onlinephpfunctions.com/code/27d4ff8d1632177db97f9e9122e7a51bd6e11146

答案 2 :(得分:0)

您需要先使用json_decode解码json。

$response = json_decode($raw);

您可以将其打印出来,以便使用print_r查看布局。

print_r($response);

正如您所看到的,它使它成为一个对象,因此您可以轻松访问这些信息。结果和内部工作空间下的ID相同。因此,您有两种解决方案。

无论

$id = $response->results[0]->id;

或者

$id = $response->workspaces->{12220207}->id;

这显然更糟,因为12220207不是静态数字。