Attempting to loop through the entire array of results but only displays one array result.


function jsonSerialize()
{
    return $this->__toArray(true);
}

$stripe = new \Stripe\StripeClient(
    $STRIPE_API_KEY
);

$link = $stripe->balanceTransactions->all(['limit' => 3]);

//$subsData = $link->jsonSerialize();
$matchUserArray = $link->jsonSerialize();
//$amount = $subsData['data'][0]['amount'];

//echo $amount;
echo $matchUserArray['data'][0]['amount'];

?>

    It's not clear what you are asking in your post, but it looks like $matchUserArray['data'] is itself an array and your code only refers to the first item in that array, $matchUserArray['data'][0]

      I want to echo out all 3 amounts from the 3 separate arrays.

      <?
      function jsonSerialize()
      {
          return $this->__toArray(true);
      }
      
      $stripe = new \Stripe\StripeClient(
          $STRIPE_API_KEY
      );
      
      $link = $stripe->balanceTransactions->all(['limit' => 3]);
      
      //$subsData = $link->jsonSerialize();
      $matchUserArray = $link->jsonSerialize();
      //$amount = $subsData['data'][0]['amount'];
      
      //echo $amount;
      echo $matchUserArray['data']['amount'];
      
      ?>
      

      var export of $matchUserArray below:

      array (
        'object' => 'list',
        'data' => 
        array (
          0 => 
          array (
            'id' => 'gadgsdfgsdfgdfs',
            'object' => 'balance_transaction',
            'amount' => -22,
            'available_on' => 1666324576,
            'created' => 1666324576,
            'currency' => 'usd',
            'description' => NULL,
            'exchange_rate' => NULL,
            'fee' => 0,
            'fee_details' => 
            array (
            ),
            'net' => -22,
            'reporting_category' => 'transfer',
            'source' => 'fadsfasdfasdfs',
            'sourced_transfers' => 
            array (
              'object' => 'list',
              'data' => 
              array (
              ),
              'has_more' => false,
              'total_count' => 0,
              'url' => '/v1/transfers?source_transaction=tr_1LvCLEJ8HvvNnh1yRFEGea3W',
            ),
            'status' => 'available',
            'type' => 'transfer',
          ),
          1 => 
          array (
            'id' => 'txn_1Lh1yIq95rV28',
            'object' => 'balance_transaction',
            'amount' => -22,
            'available_on' => 1666311005,
            'created' => 1666311005,
            'currency' => 'usd',
            'description' => NULL,
            'exchange_rate' => NULL,
            'fee' => 0,
            'fee_details' => 
            array (
            ),
            'net' => -22,
            'reporting_category' => 'transfer',
            'source' => 'tr_1Lv8oLJ8HvvbzMyJ',
            'sourced_transfers' => 
            array (
              'object' => 'list',
              'data' => 
              array (
              ),
              'has_more' => false,
              'total_count' => 0,
              'url' => '/v1/transfers?source_transaction=tr_1Lv8oLJ8HvvVbzMyJ',
            ),
            'status' => 'available',
            'type' => 'transfer',
          ),
          2 => 
          array (
            'id' => 'txn_3Lv7U8J8HvGGB6Caj',
            'object' => 'balance_transaction',
            'amount' => 248,
            'available_on' => 1666569600,
            'created' => 1666305908,
            'currency' => 'usd',
            'description' => 'fgsdfgsfdgs',
            'exchange_rate' => NULL,
            'fee' => 37,
            'fee_details' => 
            array (
              0 => 
              array (
                'amount' => 37,
                'application' => NULL,
                'currency' => 'usd',
                'description' => 'Stripe processing fees',
                'type' => 'stripe_fee',
              ),
            ),
            'net' => 211,
            'reporting_category' => 'charge',
            'source' => 'ch_3Lv7U8J8HvvNnh1y0Q4obD7b',
            'sourced_transfers' => 
            array (
              'object' => 'list',
              'data' => 
              array (
              ),
              'has_more' => false,
              'total_count' => 0,
              'url' => '/v1/transfers?source_transaction=ch_3LvD7b',
            ),
            'status' => 'pending',
            'type' => 'charge',
          ),
        ),
        'has_more' => true,
        'url' => '/v1/balance_transactions',
      )
      
      

        That error tells you that $matchUserArray['data'] has no amount key. Note that keys like amount are associative keys, whereas numbers like 0 are numeric keys. When you say you want to 'loop through the entire array of results', that suggests to me that you want to loop through a numerically indexed array. I am not at all familiar with Stripe or the StripeClient class you are using, but your initial post suggests that $matchUserArray['data'] is a numerically indexed array of results -- something you could loop through, perhaps like this:

        foreach($matchUserArray['data'] as $key => $value) {
            echo "item $key has amount value of " . $value['amount'];
        }

        It seems like you are flailing a bit because you don't know what sort of data structure is being returned. This would all be a lot easier if you could inspect the return value to examine its structure. If you have a good debugging environment, your IDE should let you do this. If not, you might consider using var_dump to show you what that data structure is, or perhaps write it to a log file like so:

        file_put_contents('/tmp/log.txt', print_r($matchUserArray['data'], TRUE))

        EDIT: I missed that code at the end of your post where you output the structure of $matchUserArray['data']. You should be able to tell from that output what the incoming data structure looks like and make the right choices in your code. If you are confused, maybe review the basics of arrays.

          Write a Reply...