0

I am using the AWS SDK on php to get instance information for remote desktop clients. I've included an example of one member of the array I'm retrieving. I'm already filtering by one of the tags offered in this array to only receive items of the group "ABC". Once this is filtered, I'm wanting to retrieve the value of the other tag known as "clientId" for each member of the array. Because the index is inconsistent, I cannot simply select the item by saying $results['Instances'][0]['Tags'][0]['Value]. Sometimes it is a different number (like $results['Instances'][0]['Tags'][1]['Value]). I would like to select it according to its sibling (Key => clientId) but I can't find a way to do that.

Thank you for any help you may provide.

This an example of one array member returned by 'describeInstances' AWS SDK:

[0] => Array
                        (
                            [Groups] => Array
                                (
                                )

                            [Instances] => Array
                                (
                                    [0] => Array
                                        (
                                            [AmiLaunchIndex] => 0
                                            [ImageId] => ami-11111111111111111
                                            [InstanceId] => i-11111111111111111
                                            [InstanceType] => t3.large
                                            [KeyName] => awskeypair_PUBLIC
                                            [LaunchTime] => Aws\Api\DateTimeResult Object
                                                (
                                                    [date] => 2019-06-22 04:08:37.000000
                                                    [timezone_type] => 2
                                                    [timezone] => Z
                                                )

                                            [Monitoring] => Array
                                                (
                                                    [State] => disabled
                                                )

                                            [Placement] => Array
                                                (
                                                    [AvailabilityZone] => us-west-1a
                                                    [GroupName] => 
                                                    [Tenancy] => default
                                                )

                                            [Platform] => windows
                                            [PrivateDnsName] => ip-111-11-11-111.us-west-1.compute.internal
                                            [PrivateIpAddress] => 111.11.11.1111                                            [ProductCodes] => Array
                                                (
                                                )

                                            [PublicDnsName] => ec2-11-11-11-11.us-west-1.compute.amazonaws.com
                                            [PublicIpAddress] => 11.11.11.11
                                            [State] => Array
                                                (
                                                    [Code] => 16
                                                    [Name] => running
                                                )

                                            [StateTransitionReason] => 
                                            [SubnetId] => subnet-1111c1f1
                                            [VpcId] => vpc-e11fb111
                                            [Architecture] => x86_64
                                            [BlockDeviceMappings] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [DeviceName] => /dev/sda1
                                                            [Ebs] => Array
                                                                (
                                                                    [AttachTime] => Aws\Api\DateTimeResult Object
                                                                        (
                                                                            [date] => 2019-05-10 14:55:20.000000
                                                                            [timezone_type] => 2
                                                                            [timezone] => Z
                                                                        )

                                                                    [DeleteOnTermination] => 1
                                                                    [Status] => attached
                                                                    [VolumeId] => vol-11111111111111111
                                                                )

                                                        )

                                                )

                                            [ClientToken] => 
                                            [EbsOptimized] => 1
                                            [EnaSupport] => 1
                                            [Hypervisor] => xen
                                            [NetworkInterfaces] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [Association] => Array
                                                                (
                                                                    [IpOwnerId] => amazon
                                                                    [PublicDnsName] => ec2-11-11-11-11.us-west-1.compute.amazonaws.com
                                                                    [PublicIp] => 11.11.11.11
                                                                )

                                                            [Attachment] => Array
                                                                (
                                                                    [AttachTime] => Aws\Api\DateTimeResult Object
                                                                        (
                                                                            [date] => 2019-05-10 14:55:19.000000
                                                                            [timezone_type] => 2
                                                                            [timezone] => Z
                                                                        )

                                                                    [AttachmentId] => eni-attach-1111111111111111
                                                                    [DeleteOnTermination] => 1
                                                                    [DeviceIndex] => 0
                                                                    [Status] => attached
                                                                )

                                                            [Description] => 
                                                            [Groups] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [GroupName] => launch-wizard-20
                                                                            [GroupId] => sg-11111111111111111
                                                                        )

                                                                )

                                                            [Ipv6Addresses] => Array
                                                                (
                                                                )

                                                            [MacAddress] => 02:5e:b0:33:b0:6e
                                                            [NetworkInterfaceId] => eni-023ccaf7dd1ade14c
                                                            [OwnerId] => 111111111111
                                                            [PrivateDnsName] => ip-111-11-11-111.us-west-1.compute.internal
                                                            [PrivateIpAddress] => 111.11.11.111
                                                            [PrivateIpAddresses] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [Association] => Array
                                                                                (
                                                                                    [IpOwnerId] => amazon
                                                                                    [PublicDnsName] => ec2-11-11-11-11.us-west-1.compute.amazonaws.com
                                                                                    [PublicIp] => 11.11.11.11
                                                                                )

                                                                            [Primary] => 1
                                                                            [PrivateDnsName] => ip-111-11-11-111.us-west-1.compute.internal
                                                                            [PrivateIpAddress] => 111.11.11.111
                                                                        )

                                                                )

                                                            [SourceDestCheck] => 1
                                                            [Status] => in-use
                                                            [SubnetId] => subnet-11111111
                                                            [VpcId] => vpc-e08fb484
                                                            [InterfaceType] => interface
                                                        )

                                                )

                                            [RootDeviceName] => /dev/sda1
                                            [RootDeviceType] => ebs
                                            [SecurityGroups] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [GroupName] => launch-wizard-20
                                                            [GroupId] => sg-1c1111111c111d17b
                                                        )

                                                )

                                            [SourceDestCheck] => 1
                                            [Tags] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [Key] => clientId
                                                            [Value] => 1234
                                                        )

                                                    [1] => Array
                                                        (
                                                            [Key] => Group
                                                            [Value] => ABC
                                                        )

                                                    [2] => Array
                                                        (
                                                            [Key] => Name
                                                            [Value] => ABC - This company's name
                                                        )

                                                )

                                            [VirtualizationType] => hvm
                                            [CpuOptions] => Array
                                                (
                                                    [CoreCount] => 1
                                                    [ThreadsPerCore] => 2
                                                )

                                            [CapacityReservationSpecification] => Array
                                                (
                                                    [CapacityReservationPreference] => open
                                                )

                                            [HibernationOptions] => Array
                                                (
                                                    [Configured] => 
                                                )

                                        )

                                )

                            [OwnerId] => 621273892911
                            [ReservationId] => r-1111111111111111
                        )
Jason Ross
  • 87
  • 7
  • Check out https://stackoverflow.com/a/6661561/3397446 – Blakethepatton Jul 29 '19 at 17:21
  • Additionally, you can pass in filters to aws's sdk. Depends on how you're accessing the data but this page is worth checking out. https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-ec2-2016-11-15.html#describeinstances – Blakethepatton Jul 29 '19 at 17:32
  • Thanks @Blakethepatton but neither of those resources address my question. I'm attempting to target an array member based on its sibling's value, not based on its own key because its own key is not unique and the path to its key is not consistent. Additionally, as mentioned in my question. I am already filtering (to the extent I can) through the AWS SDK. That is how I'm reaching this impass. – Jason Ross Jul 29 '19 at 20:27
  • You're saying that `array_search('clientId', array_column($results['Instances'][0]['Tags'], 'key'));` doesn't work? – Blakethepatton Jul 30 '19 at 20:58

1 Answers1

0
$results = [
    [
        'key' => 'Group',
        'value' => '123',
    ],
    [
        'key' => 'clientId',
        'value' => '1234',
    ],
    [
        'key' => 'Name',
        'value' => 'ABC - This company\'s name',
    ]
];

$index = array_search('clientId', array_column($results, 'key'));

if($index===false) {
    //error handling
}

var_dump($results[$index]); 

3v4l

See also: https://stackoverflow.com/a/6661561/3397446

Blakethepatton
  • 537
  • 6
  • 17