javapatriot
javapatriot

Reputation: 353

Search associative array for specific value based on set variable

I'm retrieving some JSON that I am converting to an associative array. The issue that I am having is I am trying to get the email value from the user's who id matches the value that I have already set as a variable.

Here is what the array looks like

Array
(
[object] => list
[data] => Array
    (
        [0] => Array
            (
                [object] => pro
                [id] => pro_77c9c6a85d814e059a6a2690989bae29
                [first_name] => Jane
                [last_name] => Doe
                [full_name] => Jane Doe
                [initials] => JD
                [email] => [email protected]
                [mobile_number] => 9998761234
                [messaging_uuid] => 4547c231c3e7d0ff1796f47b88f166d5
                [color_hex] => EF9159
                [avatar_url] => /assets/add_image.png
                [avatar_thumb_url] => 
                [has_avatar] => 
                [organization_name] => testorg
                [is_admin] => 1
                [permissions] => Array
                    (
                        [show_company_setup] => 1
                        [can_see_home_data] => 1
                        [show_reporting] => 1
                    )

                [is_super_pro] => 
                [is_archived] => 
                [impersonated] => 
            )

        [1] => Array
            (
                [object] => pro
                [id] => pro_0fcb8e8610e54c518078db77ced7530e
                [first_name] => Robert
                [last_name] => Jordan
                [full_name] => Robert Jordan
                [initials] => RJ
                [email] => [email protected]
                [mobile_number] => 4547457742
                [messaging_uuid] => 0fcb8e8610e54c518078db77ced7530e
                [color_hex] => EF9159
                [avatar_url] => /assets/add_image.png
                [avatar_thumb_url] => 
                [has_avatar] => 
                [organization_name] => testorg
                [is_admin] => 1
                [permissions] => Array
                    (
                        [show_company_setup] => 1
                        [can_see_home_data] => 1
                        [show_reporting] => 1
                    )

                [is_super_pro] => 
                [is_archived] => 
                [impersonated] => 
            )

    )

[url] => /pros

)

Im basically trying to match the value that I have set in my pro_id variable

pro_0fcb8e8610e54c518078db77ced7530e

To the 'ID' Key in the array above and get the 'email' value associated to that same array and store for use later in my code.

Here is what I have so far, but no joy

    foreach ($proObject as $key => $value) {
  
  if ($key['id'] == $pro_id)
    $techmail = $key['email'];
  
}

Upvotes: 0

Views: 47

Answers (1)

dajavax
dajavax

Reputation: 3400

From my understanding of your question, and if you want to get only one element, you should be able to use array_search with array_column to get the index of the searched element. Using that you can then access the element and its corresponding email value. If you might expect more than one element I would use array_filter.

One Element:

In short:

$i = array_search($pro_id, array_column($proObject, 'id'));
$element = ($i !== false ? $proObject[$i] : null);
print_r($element["email"]);

Full code:

<?php
$proObject = array(
    array(
        "object" => "pro",
        "id" => "pro_77c9c6a85d814e059a6a2690989bae29",
        "first_name" => "Jane",
        "last_name" => "Doe",
        "full_name" => "Jane Doe",
        "initials" => "JD",
        "email" => "[email protected]",
        "mobile_number" => "9998761234",
        "messaging_uuid" => "4547c231c3e7d0ff1796f47b88f166d5",
        "color_hex" => "EF9159",
        "avatar_url" => "/assets/add_image.png",
        "avatar_thumb_url" => "",
        "has_avatar" => "",
        "organization_name" => "testorg",
        "is_admin" => "1",
        "permissions" => array(
            "show_company_setup" => "1",
            "can_see_home_data" => "1",
            "show_reporting" => "1",
        ) ,

        "is_super_pro" => "",
        "is_archived" => "",
        "impersonated" => "",
    ) ,
    array(
        "object" => "pro",
        "id" => "pro_0fcb8e8610e54c518078db77ced7530e",
        "first_name" => "Robert",
        "last_name" => "Jordan",
        "full_name" => "Robert Jordan",
        "initials" => "RJ",
        "email" => "[email protected]",
        "mobile_number" => "4547457742",
        "messaging_uuid" => "0fcb8e8610e54c518078db77ced7530e",
        "color_hex" => "EF9159",
        "avatar_url" => "/assets/add_image.png",
        "avatar_thumb_url" => "",
        "has_avatar" => "",
        "organization_name" => "testorg",
        "is_admin" => "1",
        "permissions" => array(
            "show_company_setup" => "1",
            "can_see_home_data" => "1",
            "show_reporting" => "1",
        ) ,

        "is_super_pro" => "",
        "is_archived" => "",
        "impersonated" => "",
    )
);
$pro_id = "pro_0fcb8e8610e54c518078db77ced7530e";

$i = array_search($pro_id, array_column($proObject, 'id'));
$element = ($i !== false ? $proObject[$i] : null);
print_r($element["email"]);
?>

Multiple Elements:

In short:

class idEqualsFilter {
    private $id;

    public function __construct($id) {
        $this->id = $id;
    }

    function __invoke($i) {
        return $this->id === $i["id"];
    }
};
$elements = array_filter($proObject, new idEqualsFilter($pro_id));
print_r(array_column($elements, "email"));

Full code:

<?php
$proObject = array(
            array(
                "object" => "pro",
                "id" => "pro_77c9c6a85d814e059a6a2690989bae29",
                "first_name" => "Jane",
                "last_name" => "Doe",
                "full_name" => "Jane Doe",
                "initials" => "JD",
                "email" => "[email protected]",
                "mobile_number" => "9998761234",
                "messaging_uuid" => "4547c231c3e7d0ff1796f47b88f166d5",
                "color_hex" => "EF9159",
                "avatar_url" => "/assets/add_image.png",
                "avatar_thumb_url" => "",
                "has_avatar" => "",
                "organization_name" => "testorg",
                "is_admin" => "1",
                "permissions" => array
                    (
                        "show_company_setup" => "1",
                        "can_see_home_data" => "1",
                        "show_reporting" => "1",
                    ),

                "is_super_pro" => "",
                "is_archived" => "",
                "impersonated" => "",
            ),
            array(
                "object" => "pro",
                "id" => "pro_0fcb8e8610e54c518078db77ced7530e",
                "first_name" => "Robert",
                "last_name" => "Jordan",
                "full_name" => "Robert Jordan",
                "initials" => "RJ",
                "email" => "[email protected]",
                "mobile_number" => "4547457742",
                "messaging_uuid" => "0fcb8e8610e54c518078db77ced7530e",
                "color_hex" => "EF9159",
                "avatar_url" => "/assets/add_image.png",
                "avatar_thumb_url" => "",
                "has_avatar" => "",
                "organization_name" => "testorg",
                "is_admin" => "1",
                "permissions" => array
                    (
                        "show_company_setup" => "1",
                        "can_see_home_data" => "1",
                        "show_reporting" => "1",
                    ),

                "is_super_pro" => "",
                "is_archived" => "",
                "impersonated" => "",
            )
    );
$pro_id = "pro_0fcb8e8610e54c518078db77ced7530e";

class idEqualsFilter {
    private $id;

    public function __construct($id) {
        $this->id = $id;
    }

    function __invoke($i) {
        return $this->id === $i["id"];
    }
};
$elements = array_filter($proObject, new idEqualsFilter($pro_id));
print_r(array_column($elements, "email"));
?>

Upvotes: 1

Related Questions