In php, cycle through array (returned from an api) for a specific description

For a small project i am doing, i am grabbing some data from an api. the data returned is decoded as follows: under members, the first number (in this case: 65385) is the members id.

If a members status is ok, i want to return this ID, and the members name. I am able to return the members name, but not sure how to also get the ID

"members": {
    "65385": {
        "name": "phpnewb",
        "days_active": 705,
        "last_action": {
            "status": "active",
            "timestamp": "",
            "relative": "51 minutes ago"
        },
        "status": {
            "description": "Okay",
            "details": "",
            "state": "Okay",
            "color": "green",
            "until": 0
        },

Now what i want is: if under status -> Description it says “Okay” i want to return the members id & members name which would id: 65385 & name: phpnewb in this case.

Now i have tried doing this by (the name part) like this (with $idcall_array beeing the decoded data):

foreach ($idcall_array['members'] as $member) {
    
    if ($member['status']['description'] == "Okay") {
        $name = $member['name'];
        echo $name;
        echo "</br>";
    }


}

This returns the name as expected, i just have no idea how to also get the id, with a var_dump on members i don’t see the ID anymore

I am not experienced with these things.

help is appreciated

edit: when doing a var_dump($members) i get:

array(5) { ["name"]=> string(7) "phpnewb"

i asume the array(5) is the int/number i need, but dont know how to extract this.

Answer

You need to catch the key of the foreach, in your case, the id of the member :

foreach ($idcall_array['members'] as $id => $member) {
    echo $id . ' : ' . $member['name'] . '<br>';
}

NB: A correct break-line tag is <br> in HTML5.

Leave a Reply

Your email address will not be published. Required fields are marked *