0

I am trying to create a function which can process multiple different API requests from different sources dynamically. Some API objects will return the data in different ways, but I want to be able to access the data within the API object by using my $fees array.

Here is my use case:

$fees => Array
(
     [0] => data
     [1] => transaction_config
     [2] => total_fee_amount
)

I want to use this array so that I can access the data from the API Object like so:

$apiObject['data']['transaction_config']['total_fee_amount'];

Some API Objects might only be nested two deep, for example,

$fees => Array
(
     [0] => data
     [1] => fee_total
)

$apiObject['data']['fee_total'];

And I want to be able to dynamically handle this based on the contents of the fees array.

Edit: For better clarification, here is something I have tried which doesn't work

$feeKeys = '';
foreach ($fees as $feeKey) {
    $feeKeys .= "['" . $feeKey . "']";
}
$test = $apiObject . $feeKeys;

Which just returns Array['data']['transaction_config']['total_fee_amount']

Instead of the value

Jack Perry
  • 321
  • 1
  • 4
  • 13
  • 1
    Ok, what is your question though? Please post the code you have along with any errors you get – brombeer Sep 10 '21 at 06:02
  • I don't have any code, I am asking how I can achieve this. I want to use the $fees array to define the keys of the $apiObject. for example if the $fees array has a length of 5, the api object will reference each of those values. $apiObject[0][1][2][3][4]. – Jack Perry Sep 10 '21 at 06:04
  • Feels like this needs a little more information - and a little more effort from your side. Iterate over the array and create your apiobject with the values from the array. – brombeer Sep 10 '21 at 06:08
  • I'm not trying to create an api object, I'm trying to reference the data within it. – Jack Perry Sep 10 '21 at 06:20
  • 1
    Seems like you need to flatten the array but you need to be careful with duplicate keys. – nice_dev Sep 10 '21 at 06:27

1 Answers1

1

I don't know if I understood you correctly, but what you might find useful is this function:

https://stackoverflow.com/a/36334761/1055314

function flatCall($data_arr, $data_arr_call){
    $current = $data_arr;
    foreach($data_arr_call as $key){
        $current = $current[$key];
    }

    return $current;

The whole code might look like this:

<?php

$fees = [
    0 => 'data',
    1 => 'transaction_config',
    2 => 'total_fee_amount',
];

$apiObject = [
    'data' => [
        'transaction_config' => [
            'total_fee_amount' => 5
        ]
    ]
];

function flatCall($data_arr, $data_arr_call){
    $current = $data_arr;
    foreach($data_arr_call as $key){
        $current = $current[$key];
    }

    return $current;
}

$keys = array_values($fees);

$result = flatCall($apiObject, $keys);

The variable is saved in $result.

Is is what you were looking for?

slawkens
  • 138
  • 7
  • Sorry, maybe I am not explaining it well. I can't use the $apiObject like you've shown, because I don't know how many keys the value will be nested in. I'm trying to use my $fees array to define those keys. – Jack Perry Sep 10 '21 at 06:21
  • This is exactly what I'm doing in this code. You can dynamically change $fees array and $abiObject. – slawkens Sep 10 '21 at 06:26