Search code examples
phparraysredisamazon-elasticacheaws-php-sdk

Convert large array of array to associative array without loop


I am using aws redis cache for quicker results instead of saving in db. With this method

$result = $client->listTagsForResource([
    'ResourceName' => '<string>', // REQUIRED
]);

Now it gives me result in given format.

Array
(
    [0] => Array
        (
            [Key] => key1
            [Value] => string1
        )

    [1] => Array
        (
            [Key] => status
            [Value] => 1
        )

)

I am unable to find a function in amazon docs which can give me direct results, so I decided to search in array , but finding in very large array with loops cost me in terms of time. So is there a way to convert it in following

Array
(
    [key1] =>  string1,
    [status] =>  1
)

So I can directly access array index by using $array['key1']


Solution

  • You can try something like this to create new array:

    $newArray = array_combine(
                    array_column($array, 'Key'), 
                    array_column($array, 'Value')
    );
    
    echo $newArray['status'];