从JSON数组获取电子邮件

Have been trying to figure this out for a day now. I am trying to use fiken.no`s API to retrieve customernumbers. The API is HAL+JSON based and i want to access the data with PHP.

When creating a new account you receive no response, but only HTTP 201, so to get the generated customernumber i need to use the search endpoint.

That endpoint only returns all users like this:

{
"_links": {
    "self": {
        "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts"
    }
},
"_embedded": {
    "https://fiken.no/api/v1/rel/contacts": [
        {
            "_links": {
                "self": {
                    "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts/757941482"
                }
            },
            "name": "Ola Nordmann 2",
            "email": "mail@mail.com",
            "address": {
                "country": "Norge"
            },
            "customerNumber": 10003
        },
        {
            "_links": {
                "self": {
                    "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts/757941171"
                }
            },
            "name": "Ola Nordmann 1",
            "email": "findthis@example.com",
            "address": {
                "country": "Norge"
            },
            "customerNumber": 10002
        },
        {
            "_links": {
                "self": {
                    "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts/756867201"
                }
            },
            "name": "Demoleverandør",
            "address": {
                "address1": "Demoveien 44",
                "address2": "",
                "postalPlace": "Oslo",
                "postalCode": "0190",
                "country": "Norge"
            },
            "supplierNumber": 20001
        },
        {
            "_links": {
                "self": {
                    "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts/756867200"
                }
            },
            "name": "Demokunde",
            "address": {
                "address1": "Demoveien 22",
                "address2": "",
                "postalPlace": "Oslo",
                "postalCode": "0190",
                "country": "Norge"
            },
            "customerNumber": 10001
        }
    ]
}}

From this response i need to query for f.ex the email findthis@example.com and get the whole user object from that. That includes the address data and especially the customernumber. How would i go ahead to do that?

I have found this: Search a key in a Json (nested array) PHP that looks like my issues but here the key is constant in the json array. Here it is from 0 and out to infinity.

Is there any better way to process this using PHP than normal JSON practices? Thanks for any help!

First thing would be to decode the JSON into an array:

$resultArray = json_decode($yourJSONvariable, true);

Next I would recommend isolating the array of contacts from the JSON array:

$contacts = $resultArray["_embedded"]["https://fiken.no/api/v1/rel/contacts"];

Now $contacts should be an array of all the contacts from the API call. You can loop through each one to find the record with the matching email like this:

foreach ($contacts as $contact) {
    if ($contact["email"] == "findthis@example.com") {
        $mycontact = $contact;
        break;
    }
}

$mycontact will now contain an array of the contact with the matching email, and you can access its individual fields using the field names as the array index (e.g., $mycontact["name"], $mycontact["address"]["country"], $mycontact["_links"]["self"]["href"]). To see all data in the array do var_dump($contacts)