Access Values of JSON Multi Dimensional Array in PHP - php

I have attempted to parse the below ($var) using both
$convert = json_encode($var);
$object = (object)json_decode($convert);
$array = json_decode($convert, TRUE);
but appear to be unable to access values (typically seeing nothing/null), through something like $array->messages->event.
I've been cross referencing against some example code at https://gist.githubusercontent.com/eurica/6034108/raw/10e7fbce580e515db0f0b74d49c812da4e5ce40b/PagerDutyWebhookToEmail.php. However, it appears to be dated (v1 payload instead of v2).
My goal is to pull different values, at different levels, to create a custom email. Documentation suggests there could be multiple "message" under "messages" and therefore a foreach could be necessary?
X-REF https://community.pagerduty.com/t/how-do-i-notify-a-distribution-list-or-shared-group-email-address/1519 & https://v2.developer.pagerduty.com/docs/webhooks-v2-overview
{
"messages": [{
"event": "incident.trigger",
"log_entries": [
{
"id": "R2XGXEI3W0FHMSDXHDIBQGBQ5E",
"type": "trigger_log_entry",
"summary": "Triggered through the website",
"self": "https://api.pagerduty.com/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E",
"html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E",
"created_at": "2017-09-26T15:14:36Z",
"agent": {
"id": "P553OPV",
"type": "user_reference",
"summary": "Laura Haley",
"self": "https://api.pagerduty.com/users/P553OPV",
"html_url": "https://webdemo.pagerduty.com/users/P553OPV"
},
"channel": {
"type": "web_trigger",
"summary": "My new incident",
"subject": "My new incident",
"details": "Oh my gosh"
},
"service": {
"id": "PN49J75",
"type": "service_reference",
"summary": "Production XDB Cluster",
"self": "https://api.pagerduty.com/services/PN49J75",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75"
},
"incident": {
"id": "PRORDTY",
"type": "incident_reference",
"summary": "[#33] My new incident",
"self": "https://api.pagerduty.com/incidents/PRORDTY",
"html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY"
},
"teams": [
{
"id": "P4SI59S",
"type": "team_reference",
"summary": "Engineering",
"self": "https://api.pagerduty.com/teams/P4SI59S",
"html_url": "https://webdemo.pagerduty.com/teams/P4SI59S"
}
],
"contexts": [],
"event_details": {
"description": "My new incident"
}
}
],
"webhook": {
"endpoint_url": "https://requestb.in/18ao6fs1",
"name": "V2 wabhook",
"description": null,
"webhook_object": {
"id": "PN49J75",
"type": "service_reference",
"summary": "Production XDB Cluster",
"self": "https://api.pagerduty.com/services/PN49J75",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75"
},
"config": {},
"outbound_integration": {
"id": "PJFWPEP",
"type": "outbound_integration_reference",
"summary": "Generic V2 Webhook",
"self": "https://api.pagerduty.com/outbound_integrations/PJFWPEP",
"html_url": null
},
"accounts_addon": null,
"id": "PKT9NNX",
"type": "webhook",
"summary": "V2 wabhook",
"self": "https://api.pagerduty.com/webhooks/PKT9NNX",
"html_url": null
},
"incident": {
"incident_number": 33,
"title": "My new incident",
"description": "My new incident",
"created_at": "2017-09-26T15:14:36Z",
"status": "triggered",
"pending_actions": [
{
"type": "escalate",
"at": "2017-09-26T15:44:36Z"
},
{
"type": "resolve",
"at": "2017-09-26T19:14:36Z"
}
],
"incident_key": "56a1c5594fe340408230783eebf43be6",
"service": {
"id": "PN49J75",
"name": "Production XDB Cluster",
"description": "This service was created during onboarding on July 5, 2017.",
"auto_resolve_timeout": 14400,
"acknowledgement_timeout": 1800,
"created_at": "2017-07-05T17:33:09Z",
"status": "critical",
"last_incident_timestamp": "2017-09-26T15:14:36Z",
"teams": [
{
"id": "P4SI59S",
"type": "team_reference",
"summary": "Engineering",
"self": "https://api.pagerduty.com/teams/P4SI59S",
"html_url": "https://webdemo.pagerduty.com/teams/P4SI59S"
}
],
"incident_urgency_rule": {
"type": "constant",
"urgency": "high"
},
"scheduled_actions": [],
"support_hours": null,
"escalation_policy": {
"id": "PINYWEF",
"type": "escalation_policy_reference",
"summary": "Default",
"self": "https://api.pagerduty.com/escalation_policies/PINYWEF",
"html_url": "https://webdemo.pagerduty.com/escalation_policies/PINYWEF"
},
"addons": [],
"privilege": null,
"alert_creation": "create_alerts_and_incidents",
"integrations": [
{
"id": "PUAYF96",
"type": "generic_events_api_inbound_integration_reference",
"summary": "API",
"self": "https://api.pagerduty.com/services/PN49J75/integrations/PUAYF96",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75/integrations/PUAYF96"
},
{
"id": "P90GZUH",
"type": "generic_email_inbound_integration_reference",
"summary": "Email",
"self": "https://api.pagerduty.com/services/PN49J75/integrations/P90GZUH",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75/integrations/P90GZUH"
}
],
"metadata": {},
"type": "service",
"summary": "Production XDB Cluster",
"self": "https://api.pagerduty.com/services/PN49J75",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75"
},
"assignments": [
{
"at": "2017-09-26T15:14:36Z",
"assignee": {
"id": "P553OPV",
"type": "user_reference",
"summary": "Laura Haley",
"self": "https://api.pagerduty.com/users/P553OPV",
"html_url": "https://webdemo.pagerduty.com/users/P553OPV"
}
}
],
"acknowledgements": [],
"last_status_change_at": "2017-09-26T15:14:36Z",
"last_status_change_by": {
"id": "PN49J75",
"type": "service_reference",
"summary": "Production XDB Cluster",
"self": "https://api.pagerduty.com/services/PN49J75",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75"
},
"first_trigger_log_entry": {
"id": "R2XGXEI3W0FHMSDXHDIBQGBQ5E",
"type": "trigger_log_entry_reference",
"summary": "Triggered through the website",
"self": "https://api.pagerduty.com/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E",
"html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E"
},
"escalation_policy": {
"id": "PINYWEF",
"type": "escalation_policy_reference",
"summary": "Default",
"self": "https://api.pagerduty.com/escalation_policies/PINYWEF",
"html_url": "https://webdemo.pagerduty.com/escalation_policies/PINYWEF"
},
"privilege": null,
"teams": [
{
"id": "P4SI59S",
"type": "team_reference",
"summary": "Engineering",
"self": "https://api.pagerduty.com/teams/P4SI59S",
"html_url": "https://webdemo.pagerduty.com/teams/P4SI59S"
}
],
"alert_counts": {
"all": 0,
"triggered": 0,
"resolved": 0
},
"impacted_services": [
{
"id": "PN49J75",
"type": "service_reference",
"summary": "Production XDB Cluster",
"self": "https://api.pagerduty.com/services/PN49J75",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75"
}
],
"is_mergeable": true,
"basic_alert_grouping": null,
"alert_grouping": null,
"metadata": {},
"external_references": [],
"importance": null,
"incidents_responders": [],
"responder_requests": [],
"subscriber_requests": [],
"urgency": "high",
"id": "PRORDTY",
"type": "incident",
"summary": "[#33] My new incident",
"self": "https://api.pagerduty.com/incidents/PRORDTY",
"html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY"
},
"id": "69a7ced0-a2cd-11e7-a799-22000a15839c",
"created_on": "2017-09-26T15:14:36Z"
}
]}
Answer Testing:
echo $object->messages[0]->event;
Notice: Undefined property: stdClass::$messages in
/var/www/pagerduty/public_html/wh/index.php on line 251
echo $array->messages[0]->event;
Notice: Trying to get property of non-object in
/var/www/pagerduty/public_html/wh/index.php on line 255
echo $array['messages'][0]['event']
Warning: Illegal string offset 'messages' in
/var/www/pagerduty/public_html/wh/index.php on line 258
Warning: Illegal string offset 'event' in
/var/www/pagerduty/public_html/wh/index.php on line 258
{
print_r($array);
{ "messages": [ { "event": "incident.trigger", "log_entries": [ { "id": "R2XGXEI3W0FHMSDXHDIBQGBQ5E", "type": "trigger_log_entry", "summary": "Triggered through the website", "self": "https://api.pagerduty.com/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E", "html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E", "created_at": "2017-09-26T15:14:36Z", "agent": { "id": "P553OPV", "type": "user_reference", "summary": "Laura Haley", "self": "https://api.pagerduty.com/users/P553OPV", "html_url": "https://webdemo.pagerduty.com/users/P553OPV" }, "channel": { "type": "web_trigger", "summary": "My new incident", "subject": "My new incident", "details": "Oh my gosh" }, "service": { "id": "PN49J75", "type": "service_reference", "summary": "Production XDB Cluster", "self": "https://api.pagerduty.com/services/PN49J75", "html_url": "https://webdemo.pagerduty.com/services/PN49J75" }, "incident": { "id": "PRORDTY", "type": "incident_reference", "summary": "[#33] My new incident", "self": "https://api.pagerduty.com/incidents/PRORDTY", "html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY" }, "teams": [ { "id": "P4SI59S", "type": "team_reference", "summary": "Engineering", "self": "https://api.pagerduty.com/teams/P4SI59S", "html_url": "https://webdemo.pagerduty.com/teams/P4SI59S" } ], "contexts": [], "event_details": { "description": "My new incident" } } ], "webhook": { "endpoint_url": "https://requestb.in/18ao6fs1", "name": "V2 wabhook", "description": null, "webhook_object": { "id": "PN49J75", "type": "service_reference", "summary": "Production XDB Cluster", "self": "https://api.pagerduty.com/services/PN49J75", "html_url": "https://webdemo.pagerduty.com/services/PN49J75" }, "config": {}, "outbound_integration": { "id": "PJFWPEP", "type": "outbound_integration_reference", "summary": "Generic V2 Webhook", "self": "https://api.pagerduty.com/outbound_integrations/PJFWPEP", "html_url": null }, "accounts_addon": null, "id": "PKT9NNX", "type": "webhook", "summary": "V2 wabhook", "self": "https://api.pagerduty.com/webhooks/PKT9NNX", "html_url": null }, "incident": { "incident_number": 33, "title": "My new incident", "description": "My new incident", "created_at": "2017-09-26T15:14:36Z", "status": "triggered", "pending_actions": [ { "type": "escalate", "at": "2017-09-26T15:44:36Z" }, { "type": "resolve", "at": "2017-09-26T19:14:36Z" } ], "incident_key": "56a1c5594fe340408230783eebf43be6", "service": { "id": "PN49J75", "name": "Production XDB Cluster", "description": "This service was created during onboarding on July 5, 2017.", "auto_resolve_timeout": 14400, "acknowledgement_timeout": 1800, "created_at": "2017-07-05T17:33:09Z", "status": "critical", "last_incident_timestamp": "2017-09-26T15:14:36Z", "teams": [ { "id": "P4SI59S", "type": "team_reference", "summary": "Engineering", "self": "https://api.pagerduty.com/teams/P4SI59S", "html_url": "https://webdemo.pagerduty.com/teams/P4SI59S" } ], "incident_urgency_rule": { "type": "constant", "urgency": "high" }, "scheduled_actions": [], "support_hours": null, "escalation_policy": { "id": "PINYWEF", "type": "escalation_policy_reference", "summary": "Default", "self": "https://api.pagerduty.com/escalation_policies/PINYWEF", "html_url": "https://webdemo.pagerduty.com/escalation_policies/PINYWEF" }, "addons": [], "privilege": null, "alert_creation": "create_alerts_and_incidents", "integrations": [ { "id": "PUAYF96", "type": "generic_events_api_inbound_integration_reference", "summary": "API", "self": "https://api.pagerduty.com/services/PN49J75/integrations/PUAYF96", "html_url": "https://webdemo.pagerduty.com/services/PN49J75/integrations/PUAYF96" }, { "id": "P90GZUH", "type": "generic_email_inbound_integration_reference", "summary": "Email", "self": "https://api.pagerduty.com/services/PN49J75/integrations/P90GZUH", "html_url": "https://webdemo.pagerduty.com/services/PN49J75/integrations/P90GZUH" } ], "metadata": {}, "type": "service", "summary": "Production XDB Cluster", "self": "https://api.pagerduty.com/services/PN49J75", "html_url": "https://webdemo.pagerduty.com/services/PN49J75" }, "assignments": [ { "at": "2017-09-26T15:14:36Z", "assignee": { "id": "P553OPV", "type": "user_reference", "summary": "Laura Haley", "self": "https://api.pagerduty.com/users/P553OPV", "html_url": "https://webdemo.pagerduty.com/users/P553OPV" } } ], "acknowledgements": [], "last_status_change_at": "2017-09-26T15:14:36Z", "last_status_change_by": { "id": "PN49J75", "type": "service_reference", "summary": "Production XDB Cluster", "self": "https://api.pagerduty.com/services/PN49J75", "html_url": "https://webdemo.pagerduty.com/services/PN49J75" }, "first_trigger_log_entry": { "id": "R2XGXEI3W0FHMSDXHDIBQGBQ5E", "type": "trigger_log_entry_reference", "summary": "Triggered through the website", "self": "https://api.pagerduty.com/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E", "html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E" }, "escalation_policy": { "id": "PINYWEF", "type": "escalation_policy_reference", "summary": "Default", "self": "https://api.pagerduty.com/escalation_policies/PINYWEF", "html_url": "https://webdemo.pagerduty.com/escalation_policies/PINYWEF" }, "privilege": null, "teams": [ { "id": "P4SI59S", "type": "team_reference", "summary": "Engineering", "self": "https://api.pagerduty.com/teams/P4SI59S", "html_url": "https://webdemo.pagerduty.com/teams/P4SI59S" } ], "alert_counts": { "all": 0, "triggered": 0, "resolved": 0 }, "impacted_services": [ { "id": "PN49J75", "type": "service_reference", "summary": "Production XDB Cluster", "self": "https://api.pagerduty.com/services/PN49J75", "html_url": "https://webdemo.pagerduty.com/services/PN49J75" } ], "is_mergeable": true, "basic_alert_grouping": null, "alert_grouping": null, "metadata": {}, "external_references": [], "importance": null, "incidents_responders": [], "responder_requests": [], "subscriber_requests": [], "urgency": "high", "id": "PRORDTY", "type": "incident", "summary": "[#33] My new incident", "self": "https://api.pagerduty.com/incidents/PRORDTY", "html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY" }, "id": "69a7ced0-a2cd-11e7-a799-22000a15839c", "created_on": "2017-09-26T15:14:36Z" } ] }

but appear to be unable to access values (typically seeing nothing/null), through something like $array->messages->event.
You can't access $array (that you received through json_decode($convert, true) like that.
Try accessing them like $array['messages'][0]['event']

Related

Elasticsearch batch not indexing all items using PHP client

I've been trying to build an index in ES and add the initial items to it (around 350k), using PHP.
I tried all kinds of batch sizes (from 10 items to 1k), check the count, check the threshold, but for some reason it doesn't index every item.
It just skips over some random items, without any errors in the batch result response. I feel like I tried everything and I have to idea what to do next
I'm using Amazon OpenSearch with the latest supported ES (7.10).
The index looks like this:
{
"wonder-search": {
"aliases": {},
"mappings": {
"properties": {
"address": {
"type": "text"
},
"city": {
"type": "text"
},
"city_id": {
"type": "integer"
},
"duration": {
"type": "integer"
},
"filename": {
"type": "text"
},
"geo_point": {
"type": "geo_point"
},
"icon": {
"type": "keyword"
},
"is_sandbox": {
"type": "integer"
},
"item_id": {
"type": "integer"
},
"item_label": {
"type": "keyword"
},
"latitude": {
"type": "float"
},
"longitude": {
"type": "float"
},
"search_text_caption_json": {
"type": "text",
"index_phrases": true
},
"search_text_city_json": {
"type": "text",
"index_phrases": true
},
"search_text_completion": {
"type": "completion",
"analyzer": "simple",
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50,
"contexts": [
{
"name": "type",
"type": "CATEGORY"
}
]
},
"search_text_country_json": {
"type": "text",
"index_phrases": true
},
"search_text_cuisine_name_json": {
"type": "text",
"index_phrases": true
},
"search_text_location_name_json": {
"type": "text",
"index_phrases": true
},
"search_text_state_json": {
"type": "text",
"index_phrases": true
},
"search_text_tag_name_json": {
"type": "text",
"index_phrases": true
},
"search_text_username_json": {
"type": "text",
"index_phrases": true
},
"sort": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"sort_score": {
"type": "double"
},
"type": {
"type": "text"
},
"user_icon": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user_id": {
"type": "integer"
},
"username": {
"type": "keyword"
},
"vanity_url": {
"type": "keyword"
},
"video_count": {
"type": "integer"
}
}
},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"mapping": {
"ignore_malformed": "true"
},
"number_of_shards": "1",
"provided_name": "wonder-search",
"creation_date": "1671003076106",
"number_of_replicas": "1",
"uuid": "YQh1q40WTneLE4MWDWhArw",
"version": {
"created": "7100199"
}
}
}
}
}
and one item looks like this:
{
"_index": "wonder-search",
"_type": "_doc",
"_id": "wq2LD4UBUAuy7FQPhtZh",
"_version": 1,
"_seq_no": 2003,
"_primary_term": 1,
"found": true,
"_source": {
"sort": "4004",
"item_id": "4934",
"user_id": "434",
"user_icon": "/site-content/avatars/Sp8AXjTJvMbRao2oZbuiUuSVH042-1597776099045.jpeg",
"username": "chuurros",
"item_label": "Kyoto Katsugyu【京都勝牛】",
"search_text_username_json": [
"chuurros"
],
"search_text_caption_json": [
"Absolutely love their gyukatsu (beef katsu) here! Delicious and will keep you wanting more! 🥰"
],
"search_text_city_json": [
"Toronto"
],
"search_text_state_json": [
"Ontario"
],
"search_text_country_json": [
"Canada"
],
"search_text_location_name_json": [
"Kyoto Katsugyu【京都勝牛】"
],
"search_text_tag_name_json": [
"japanese",
"restaurant",
"asian",
"dining",
"topcollection-4934"
],
"search_text_cuisine_name_json": [],
"type": "video",
"vanity_url": "",
"icon": "",
"city": "Toronto",
"city_id": "439",
"latitude": "43.65682410",
"longitude": "-79.37617410",
"address": "134 Dundas St E",
"duration": "9.57",
"video_count": "0",
"sort_score": "43",
"filename": "373d75fd-4292-4e5b-a239-4b1c39ffc86c.MOV",
"is_sandbox": "0",
"geo_point": {
"lat": "43.65682410",
"lon": "-79.37617410"
},
"search_text_completion": {
"input": [
"Kyoto Katsugyu【京都勝牛】"
],
"contexts": {
"type": [
"video"
]
}
}
}
}
Any ideas why does it work like this?

How to read Specific value from curl Response

this is my response
i need to read first line id value & url of sandbox
{
"id": "chg_g5Y25220190539Ob590811651",
"object": "charge",
"live_mode": false,
"api_version": "V2",
"method": "CREATE",
"status": "INITIATED",
"amount": 1.000,
"currency": "KWD",
"threeDSecure": false,
"card_threeDSecure": false,
"save_card": false,
"statement_descriptor": "Sample",
"description": "Test DESC",
"transaction":
{
"timezone": "UTC+03:00",
"created": "1573191592651",
"url": "https://sandbox.payments.tap.company/test_gosell/v2/payment/response.aspx?tap_chg=PsV8hZdr78kEIAOZl9XL1B5u3dXe%2bBQ%2b3Hrd%2fempxYs%3d&sess=noWi606EI%2bM%3d&token=PsV8hZdr78kEIAOZl9XL1B5u3dXe%2bBQ%2bTyYdQy9iGpS6aAXwKOqAdw%3d%3d",
"expiry": { "period": 30, "type": "MINUTE" },
"asynchronous": false
},
"reference": { "transaction": "txn_0001", "order": "ord_0001" },
"response": { "code": "100", "message": "Initiated" },
"receipt": { "email": true, "sms": true },
"customer": { "first_name": "yxy", "last_name": "ttt", "email": "xyx#xyz.com" },
"source": { "object": "source", "id": "src_card" },
"redirect": { "status": "PENDING", "url": "http://localhost:81/School/" },
"post": { "status": "PENDING", "url": "http://localhost:81/School/" } }
assuming you have your response from curl in a variable called $resp
$resp_decoded = json_decode($resp,TRUE);
print($resp_decoded['id']);
print($resp_decoded['transaction']['url']);

Podio api - how to set category field when create item

I am using php wrapper, try create an item, all is ok, item is creating, but I can't change status, tried different ways, but can't find right way.
Need change status to "Closed" - http://prntscr.com/fsrwc3
Codes not works:
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>13]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
or
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>13]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
$get_item = PodioItem::get_basic($item->item_id);
$get_item->fields['status']->values = ['id'=>13];
$get_item->save();
After create item (after new PodioItem...), if I get fields after this code (just get $item->fields or PodioItem::get_basic...), I can see correct status, only in the code, only immediatly after create item, but if I get this item in the another code (just PodioItem::get_basic...) I will see default value, so code don't change status, looks like I just see some cache.
If I create item on the one script:
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>13]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
Then, in the another script update field, it will be change:
$get_item = PodioItem::get_basic('639637317');
$get_item->fields['status']->values = ['id'=>13];
$get_item->save();
Update - debug information:
Get log by test code -
PodioItem::create($app_id, ['fields' => ['status'=>[13], 'category'=>[3], 'contract-type'=>[4]]]);
(simple variant for creating an item, have the same problems like new PodioItem... )
All fields have category type, but:
status - have inline show type - don't chnages
category - have dropdown show type - Is changing
contract-type - have dropdown show type - Is changing
Log:
2017-07-08 11:07:22 200 POST /item/app/12152727/
2017-07-08 11:07:22 Request body: {"fields":{"status":[8],"category":[3],"contract-type":[4]}}
2017-07-08 11:07:22 Reponse: {
"presence": {
"ref_type": "item",
"ref_id": 641331142,
"user_id": 4194774,
"signature": "c165b85090e6ad28e74ae4baf93ee56113f88bc9"
},
"app": {
"status": "active",
"sharefile_vault_url": null,
"name": "Projects",
"default_view_id": null,
"url_add": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects\/items\/new",
"icon_id": 378,
"link_add": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects\/items\/new",
"app_id": 12152727,
"current_revision": 141,
"item_name": "Project",
"link": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects",
"url": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects",
"url_label": "projects",
"config": {
"item_name": "Project",
"icon_id": 378,
"type": "standard",
"name": "Projects",
"icon": "378.png"
},
"space_id": 3466816,
"icon": "378.png"
},
"created_on": "2017-07-10 15:31:16",
"last_event_on": "2017-07-10 15:31:16",
"linked_account_data": null,
"sharefile_vault_folder_id": null,
"app_item_id_formatted": "3988",
"recurrence": null,
"title": "ACSC",
"participants": {},
"created_by": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"priority": 641331142.0,
"created_via": {
"url": null,
"auth_client_id": 25162,
"display": true,
"name": "importer",
"id": 25162
},
"subscribed_count": 1,
"reminder": null,
"ref": null,
"revision": 0,
"app_item_id": 3988,
"link": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects\/items\/3988",
"item_id": 641331142,
"sharefile_vault_url": null,
"rights": ["subscribe", "grant", "add_conversation", "rate", "update", "delete", "add_file", "grant_view", "view", "comment", "add_task"],
"fields": [{
"status": "active",
"type": "category",
"field_id": 93352415,
"label": "Division",
"values": [{
"value": {
"status": "active",
"text": "ACSC",
"id": 3,
"color": "D2E4EB"
}
}],
"config": {
"default_value": null,
"unique": false,
"description": null,
"hidden_create_view_edit": false,
"required": true,
"mapping": null,
"label": "Division",
"visible": true,
"delta": 5,
"hidden": false,
"settings": {
"multiple": false,
"options": [{
"status": "active",
"text": "ACSE",
"id": 1,
"color": "DCEBD8"
}, {
"status": "active",
"text": "ACSB",
"id": 2,
"color": "F7F0C5"
}, {
"status": "active",
"text": "ACSC",
"id": 3,
"color": "D2E4EB"
}],
"display": "dropdown"
}
},
"external_id": "category"
}, {
"status": "active",
"type": "category",
"field_id": 148215928,
"label": "Contract Type",
"values": [{
"value": {
"status": "active",
"text": "No Contract",
"id": 4,
"color": "DDDDDD"
}
}],
"config": {
"default_value": null,
"unique": false,
"description": null,
"hidden_create_view_edit": false,
"required": true,
"mapping": null,
"label": "Contract Type",
"visible": true,
"delta": 7,
"hidden": false,
"settings": {
"multiple": false,
"options": [{
"status": "active",
"text": "PO \/ Purchase \/ T&M",
"id": 1,
"color": "FFD5C2"
}, {
"status": "active",
"text": "Original Contract",
"id": 2,
"color": "D2E4EB"
}, {
"status": "active",
"text": "Service Rider",
"id": 3,
"color": "DCEBD8"
}, {
"status": "active",
"text": "No Contract",
"id": 4,
"color": "DDDDDD"
}],
"display": "dropdown"
}
},
"external_id": "contract-type"
}, {
"status": "active",
"type": "category",
"field_id": 93034840,
"label": "Instal Status",
"values": [{
"value": {
"status": "active",
"text": "Closed",
"id": 13,
"color": "E1D8ED"
}
}],
"config": {
"default_value": null,
"unique": false,
"description": null,
"hidden_create_view_edit": false,
"required": true,
"mapping": null,
"label": "Instal Status",
"visible": true,
"delta": 13,
"hidden": false,
"settings": {
"multiple": false,
"options": [{
"status": "deleted",
"text": "To Be Reviewed",
"id": 2,
"color": "F7F0C5"
}, {
"status": "deleted",
"text": "ACS Accounting Review",
"id": 5,
"color": "FFD5C2"
}, {
"status": "deleted",
"text": "Ignite Setup",
"id": 10,
"color": "DCEBD8"
}, {
"status": "active",
"text": "To Be Scheduled",
"id": 8,
"color": "D2E4EB"
}, {
"status": "active",
"text": "In Progress",
"id": 6,
"color": "DCEBD8"
}, {
"status": "active",
"text": "Warranty",
"id": 11,
"color": "F7F0C5"
}, {
"status": "active",
"text": "Complete",
"id": 3,
"color": "D1F3EC"
}, {
"status": "deleted",
"text": "Closed",
"id": 7,
"color": "DDDDDD"
}, {
"status": "active",
"text": "Cancelled",
"id": 9,
"color": "DDDDDD"
}, {
"status": "deleted",
"text": "VA in Progress",
"id": 4,
"color": "E1D8ED"
}, {
"status": "deleted",
"text": "Submitted",
"id": 1,
"color": "F7F0C5"
}, {
"status": "active",
"text": "On Hold",
"id": 12,
"color": "F7D1D0"
}, {
"status": "active",
"text": "Closed",
"id": 13,
"color": "E1D8ED"
}],
"display": "inline"
}
},
"external_id": "status"
}],
"initial_revision": {
"item_revision_id": 1664054437,
"created_via": {
"url": null,
"auth_client_id": 25162,
"display": true,
"name": "importer",
"id": 25162
},
"created_by": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"created_on": "2017-07-10 15:31:16",
"user": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"type": "creation",
"revision": 0
},
"current_revision": {
"item_revision_id": 1664054437,
"created_via": {
"url": null,
"auth_client_id": 25162,
"display": true,
"name": "importer",
"id": 25162
},
"created_by": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"created_on": "2017-07-10 15:31:16",
"user": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"type": "creation",
"revision": 0
},
"linked_account_id": null,
"push": {
"timestamp": 1499700676,
"expires_in": 21600,
"channel": "\/item\/641331142",
"signature": "b8a816ff367da6bc730071c875ca3fdca2d2c344"
},
"external_id": null
}
Have you tried http://podio.github.io/podio-php/fields/#category-field ?
Setting values
Set a single value by using the option_id. You can also
add a value with add_value()
$item = PodioItem::get_basic(123);
$field_id = 'category';
// Set value to a single option
$item->fields[$field_id]->values = 4; // option_id=4
// Add value to existing selection
$item->fields[$field_id]->add_value(4); // option_id=4
Use an array to set multiple values
$item = PodioItem::get_basic(123);
$field_id = 'category';
$item->fields[$field_id]->values = array(4,5,6); // option_ids: 4, 5 and 6
Creating item with value:
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>array(13)]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();

REGEX: needed in php [duplicate]

This question already has an answer here:
How to extract individual elements from a JSON string?
(1 answer)
Closed 5 years ago.
{
"status": "success",
"data": {
"title": "test2",
"json_query": {
"condition": "AND",
"rules": [{
"id": "event",
"field": "event",
"type": "string",
"operator": "equal",
"value": "signup"
}, {
"condition": "AND",
"rules": [{
"id": "event",
"field": "event",
"type": "string",
"operator": "equal",
"value": "signup"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "epub"
}]
}]
}, {
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "arts"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "automotive"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "books"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "business"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "books"
}]
}]
}]
}]
}]
}
}
}
This is my string and i need the regex which will get all id with its value
eg: "id" : "event", "id" : "event", "id" : "kwd", "id" : "kwd"
means every occurrence, so i can replace or add a unique value to id
NOTE:- I dont want to make an array, dont want to do with loop,
finding possibility to replace with preg_match or any other regex method.
I got my answer myself:
preg_match_all('/"id":"(.*)",/U', $json, $matches, PREG_PATTERN_ORDER);
print_r($matches); die;

extract data value from nested JSON

I am trying to get some specific fields out of this Json.
fn({
"processingDurationMillis": 454,
"authorisedAPI": true,
"success": true,
"airline": "MH",
"validCreditCards": [
"AX",
"CA",
"VI"
],
"paypal": true,
"outboundOptions": [
{
"optionId": 0,
"flights": [
{
"flightNumber": "0066",
"departureAirport": {
"code": "KUL",
"name": "Kuala Lumpur Intl",
"city": "Kuala Lumpur",
"country": "Malaysia",
"timezone": "Asia/Kuala_Lumpur",
"lat": 2.745578,
"lng": 101.709917,
"terminal": null,
"gate": null
},
"arrivalAirport": {
"code": "ICN",
"name": "Incheon Intl",
"city": "Seoul",
"country": "South Korea",
"timezone": "Asia/Seoul",
"lat": 37.469075,
"lng": 126.450517,
"terminal": null,
"gate": null
},
"marketingAirline": "MH",
"mealIndicator": "M",
"allowedSsrs": {
},
"operatingAirline": null,
"equipment": "333",
"equipmentName": "Airbus A330-300",
"flightRPH": 10101,
"comments": [
"MH CODESHARE WITH KE"
],
"depScheduled": "2015-04-28T23:30:00.000+08:00",
"arrScheduled": "2015-04-29T07:10:00.000+09:00",
"depEstimated": null,
"depActual": null,
"arrEstimated": null,
"arrActual": null,
"eligibleForEticketing": true,
"cabin": "ECONOMY",
"fareMarketingType": "BASIC",
"rbd": "N",
"seatsAvailable": 9,
"durationMinutes": 400,
"minutesToScheduledFlightDeparture": 6486
}
],
"stopOvers": [
],
"fareDetails": {
"perPassengerJourneyFares": [
{
"passengerType": "ADT",
"fare": "1214.95",
"currencyCode": "MYR"
}
],
"perPassengerTripTaxes": [
{
"passengerType": "ADT",
"totalTax": "68.90",
"taxes": [
{
"code": "MY",
"amount": "65.00",
"currency": "MYR"
},
{
"code": "D8",
"amount": "3.90",
"currency": "MYR"
}
]
}
],
"journeyFare": "1214.95",
"totalTripFare": "1283.90",
"fareCurrency": "MYR"
},
"magicString": "2t2qi8oNXWkrDR75zYDrk9+3wNaJBzHyK1ftoR/VZPVgHO+EFTkh8DMg5WUl1ap7VjwBsnhD2gFxAwBbHhY0+k0lp7BUvSoYSKg6S6u4ZkvbIWMktl+lHgcKl46vht9//2dZVJvH4D7WJvnJTtK5O4TWNrkiTmEdHp55yRmjwWfsgNswOIMXoWrZj3OUJ4DH4POJ8rmfilimvtpBCdxNsqoZDVC9d6/6LiICZ3wHZJ7w/88QuExFV7OsHbc+jI3trRzDCCb6Ns62MGyfsXX6Pz8mJe6gs02UjapVSPa3M9CqLGMCN0xCF28WNbavhSI9jG3cWsQbxGU8rnhmjx00Iw5v2qqjdE/Dx432Qzs4s36SqUjLF7KN9hAJoQuMX3emE4gZ+7ANJ5bDTDEYZlnUZ4iXKykzUptYDyGay0evu1kdCjxPJlgiEtOl3hFMaKC+eoTsjps4RoYy0Z7oD3aP52qCYPdCH+8XTic522UKU1mW9HMjmGxH5zrvYK2rOgzSR2+xH5K3IpXHBAQqWOTEvmirP4qvg5VOPjyO9mIM83I6aY1JAkqo9jYqtEwrGqANdhA9z78EdoyQYKZBXcLsQMKz06fAczwk/WxxIi1ctL8EW+aZYddkbPo7xD6NWc8bJ+ARw5AlS1tirVNcO3mN5jVr/a6qftVuaz/0q83VsX4ztQpgMjDkptbw9Zz6DNLgiLJEzdf7fraoVUyzeth5wucOMzpLBP+ERbD7XFnDSKN8QzG6lLpDK8qy95K5FMmcF4uDq8Y1waTyIN9sS+v50OTbjr7Ebs3uKIxMZFfGUfp7YpDiVyo+2x4La4K7rhHPtoR6iEfVCjnTAUvamQu3qgL3vuSCPPPJiHFbdOrKVlp3kfAxaIcJpX3Z+Twx2cNAhsGHSk2ZazzvP5Pw1EF066VcoDkld9Oe/Qu5cC+DtG2LHhMA7NU8hMD66q9UCsXC6P/mjbKr7hatjHyyklDIKuxxirMpYkukEa73RJlhKmC0fjj4EYcgRy5MtybexuN59KaTeSEFxMGFIkv0zHp5jO/wHUvyypqbxTKFR3VAx6WpmSNg/Iui2uXDhNu/F4zJnYQUW9EyluZEPebFk2Uj455O2+y0UmFe4WnUY+0d92obZNv855/ctA4UC/LQn2s9azqdhDIeUUHuHEn2a4Grb+7l8wuai6ybBmmE62ck+CqMou+A+CUwk71KMkh3ZXf8BdeelW8Ia7r9ja7wKNBklgYo4Q8xOR63QhyCt2BiiR9aOxiDIKiW7bxSFCBga7yIPWx/NZdGjUYTuiJ9KZ7W2dKLhF6XDU5mWOV7XwMRzkyschEnjSzQWGjTTftEIiNI1V1M2bhFwc92JkfVFxwXCg==",
"seatsAvailable": [
9
],
"corporateAccount": false,
"flightCanBeHeld": true,
"durationMinutes": 400,
"gaFareDetails": {
"perPassengerJourneyFares": [
{
"passengerType": "ADT",
"fare": "1214.95",
"currencyCode": "MYR"
}
],
"perPassengerTripTaxes": [
{
"passengerType": "ADT",
"totalTax": "68.90",
"taxes": [
{
"code": "MY",
"amount": "65.00",
"currency": "MYR"
},
{
"code": "D8",
"amount": "3.90",
"currency": "MYR"
}
]
}
],
"journeyFare": "1214.95",
"totalTripFare": "1283.90",
"fareCurrency": "MYR"
},
"adobeFareDetails": {
"perPassengerJourneyFares": [
{
"passengerType": "ADT",
"fare": "336.66",
"currencyCode": "USD"
}
],
"perPassengerTripTaxes": [
{
"passengerType": "ADT",
"totalTax": "19.09",
"taxes": [
{
"code": "MY",
"amount": "18.01",
"currency": "USD"
},
{
"code": "D8",
"amount": "1.08",
"currency": "USD"
}
]
}
],
"journeyFare": "336.66",
"totalTripFare": "355.77",
"fareCurrency": "USD"
},
"userAgentFareDetails": {
"perPassengerJourneyFares": [
{
"passengerType": "ADT",
"fare": "336.66",
"currencyCode": "USD"
}
],
"perPassengerTripTaxes": [
{
"passengerType": "ADT",
"totalTax": "19.09",
"taxes": [
{
"code": "MY",
"amount": "18.01",
"currency": "USD"
},
{
"code": "D8",
"amount": "1.08",
"currency": "USD"
}
]
}
],
"journeyFare": "336.66",
"totalTripFare": "355.77",
"fareCurrency": "USD"
},
"eligibleForeTicketing": true,
"lowestSeatCount": 9,
"directFlight": true
}
],
"departureAirport": {
"code": "KUL",
"name": "Kuala Lumpur Intl",
"city": "Kuala Lumpur",
"country": "Malaysia",
"timezone": "Asia/Kuala_Lumpur",
"lat": 2.745578,
"lng": 101.709917,
"terminal": null,
"gate": null
},
"arrivalAirport": {
"code": "ICN",
"name": "Incheon Intl",
"city": "Seoul",
"country": "South Korea",
"timezone": "Asia/Seoul",
"lat": 37.469075,
"lng": 126.450517,
"terminal": null,
"gate": null
},
"apiRequired": true,
"fareRules": [
{
"id": 50,
"order": 1,
"priority": 0,
"code": "Basic",
"name": "MHbasic",
"value": "Economy Class Fares",
"listFareRules": [
{
"id": 130,
"order": 0,
"code": "",
"name": "Discount level",
"value": "Up to 65%"
},
{
"id": 140,
"order": 1,
"code": "",
"name": "Where to buy",
"value": "All channels"
},
{
"id": 150,
"order": 2,
"code": "",
"name": "Advance purchase",
"value": "Applies"
},
{
"id": 160,
"order": 3,
"code": "",
"name": "Payment",
"value": "Ticket dateline applies"
},
{
"id": 170,
"order": 4,
"code": "",
"name": "Baggage allowance",
"value": "2pc/30kg"
},
{
"id": 180,
"order": 5,
"code": "",
"name": "Advance seat selection",
"value": "Not allowed"
},
{
"id": 190,
"order": 6,
"code": "",
"name": "Enrich miles",
"value": "Nil"
},
{
"id": 200,
"order": 7,
"code": "",
"name": "Change of booking",
"value": "Not allowed"
},
{
"id": 210,
"order": 8,
"code": "",
"name": "Upgrade",
"value": "Not allowed"
},
{
"id": 220,
"order": 9,
"code": "",
"name": "Stand by at the airport",
"value": "For a fee"
},
{
"id": 220,
"order": 10,
"code": "",
"name": "No show",
"value": "Penalty applies"
},
{
"id": 230,
"order": 11,
"code": "",
"name": "Refund",
"value": "For a fee"
}
],
"listFareNotes": [
{
"id": 10,
"order": 0,
"code": "",
"name": "Important Notice",
"value": ""
},
{
"id": 15,
"order": 1,
"code": "",
"name": "",
"value": ""
},
{
"id": 20,
"order": 2,
"code": "",
"name": "1.",
"value": "Generic attributes shown only applies to MH operated flights. MH3000-3999, MH5200-5999 and MH9000-9999 Series flights are subject to their own rules. Please contact MH Call Center or ticket offices for actual fare rules."
},
{
"id": 30,
"order": 3,
"code": "",
"name": "2.",
"value": "For transpacific and transatlantic flights, the following baggage allowances apply: Economy - 2 pieces (23kg each piece), First and Business - 2 pieces (32kg each piece)."
},
{
"id": 50,
"order": 5,
"code": "",
"name": "3.",
"value": "Upgrade, standby at the airport and refund fees for specific routes can be obtained from subsequent booking pages."
},
{
"id": 60,
"order": 6,
"code": "",
"name": "4.",
"value": "Standby at the airport denotes the same day for an earlier flight."
},
{
"id": 70,
"order": 7,
"code": "",
"name": "5.",
"value": "Fare rules shown are indicative only. Please call our Contact Center to check the detailed fare rules."
},
{
"id": 80,
"order": 8,
"code": "",
"name": "",
"value": "Should there be any discrepancy between the above information and the terms and conditions (T&C) published in the fare rules, then the T&C in the fare rules shall prevail."
}
]
}
],
"Errors": [
],
"Warnings": [
]
})
i want extract flight number, depScheduled, arrScheduled and journey fare from the above json.
here are my code:
$json2 = json_decode($json,true);
$result= array();
foreach ($json2['outboundOptions']['flights']as $theentity) {
$result[] = $theentity['flightNumber'];
}
print_r($result);
The code above return me a error, "Invalid argument supplied for foreach()".I searched around, but still have not found the solution yet..
It is giving you that error because outboundOptions is an array of objects. What you want is to access the first object:
foreach ($json2['outboundOptions'][0]['flights']as $theentity) {
$result[] = $theentity['flightNumber'];
}
Also, remove the trailing comma (,) from your ] at the end as that causes invalid json.
You can check if your json is valid by going to jsonlint.com
Working Example
Update as per your comment
To get all the flights, change your foreach loop to this:
foreach ($json3['outboundOptions'] as $flight) {
foreach($flight['flights'] as $theentity) {
$result[] = $theentity['flightNumber'];
}
}
Example
remove the trailing ',' near the very end of your json.
change your code, add a [0] before ['flights']
$json2 = json_decode($json,TRUE);
$result= array();
foreach ($json2['outboundOptions'][0]['flights']as $theentity) {
$result[] = $theentity['flightNumber'];
}
print_r($result);

Categories