Answer the question
In order to leave comments, you need to log in
How to get the value of the parent element of a php array given one of the values of its child array?
Recently I left a question about how to get the contact id knowing his phone number in AmoCrm using api.
Figured out how to get a list of all contacts.
When sending a request to https://'.$subdomain.'.amocrm.ru/private/api/v2/json/contacts/list, the following is returned:
Please help extract the value of the id field whose phone number matches the known one (we get from the form).
(+3 (333) 333-33-33 - phone number)
code below is json printout (fesor user hint);
$out=curl_exec($curl);
print_r($out;)
{
"response": {
"contacts": [
{
"id": 50824019,
"name": "Zecuch",
"last_modified": 1413915416,
"account_id": 7975286,
"date_create": 1413915415,
"created_user_id": 294474,
"responsible_user_id": 294474,
"linked_leads_id": [],
"group_id": 0,
"tags": [],
"company_name": "",
"linked_company_id": null,
"type": "contact",
"custom_fields": [
{
"id": "1279390",
"name": "Email",
"code": "EMAIL",
"values": [
{
"value": "[email protected]",
"enum": "2942838"
}
]
},
{
"id": "1279388",
"name": "Телефон",
"code": "PHONE",
"values": [
{
"value": "+3 (333) 333-33-34",
"enum": "2942836"
}
]
}
]
},
{
"id": 50824011,
"name": "Ястр",
"last_modified": 1413915437,
"account_id": 7975286,
"date_create": 1413915319,
"created_user_id": 294474,
"responsible_user_id": 294474,
"linked_leads_id": [],
"group_id": 0,
"tags": [],
"company_name": "",
"linked_company_id": null,
"type": "contact",
"custom_fields": [
{
"id": "1279390",
"name": "Email",
"code": "EMAIL",
"values": [
{
"value": "[email protected]",
"enum": "2942838"
}
]
},
{
"id": "1279388",
"name": "Телефон",
"code": "PHONE",
"values": [
{
"value": "+3 (333) 333-33-33",
"enum": "2942836"
}
]
}
]
}
],
"server_time": 1413922440
}
}
Answer the question
In order to leave comments, you need to log in
As stated in the documentation , you can use the "query" parameter in the query, which allows you to search by field values.
If you specify the desired phone number as the value of this parameter, then only those contacts will be in the output list of contacts who have this phone number in one of the fields.
Unfortunately, apparently, only the exact match is used: if the desired phone is written without brackets or spaces, nothing will be found.
Although, if, according to your own rules, you normalize phone numbers when writing to CRM, then you can search.
function array_some(array $collection, $search) {
$filtered = array_filter($collection, $search);
return empty($filtered) ? null : $filtered[0];
}
function find_contact(array $collection, $phone) {
return array_some($collection, function ($contact) use ($phone) {
return null !== array_some($contact['custom_fields'], function($field) use ($phone) {
return $field['code'] === 'PHONE'
&& null !== some($field['values'], function ($value) use ($phone) {
return $value['value'] === $phone;
});
});
});
}
$response = json_decode($jsonResponse, true);
$contact = findContact($response['response'], '+3 (333) 333-33-33');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question