2

I am having problems getting the php 'foreach' to work with a Stripe object.

i can run the following command which gives me expected results:

error_log(gettype($event->data->object->metadata));   // displays 'Stripe\StripeObject Object'

then i can run this:

error_log(print_r($event->data->object->metadata, true));

which displays:

(
    [last_name] => XXXXX
    [gateway_id] => stripe_cc
    [user_agent] => Mozilla/5.0  . . . . . . .Chrome/141.0.0.0 Safari/537.36
    [webhook_id] => we_XZXXXXd0n
    [first_name] => XXX
    .......................
    [ip_address] => 123.123.123.123
)

and i can even extract values using something like this:

$event->data->object->metadata['ip_address']  // returns ip address
$event->data->object->metadata['first_name'] // returns first name

however, for whatever reason, i cannot seem to get the following foreach statements to produce anything:

foreach ($event->data->object->metadata as $key => $value )    {
    error_log( $key  . ' => ' $value );
}
foreach ( (array) $event->data->object->metadata as $key => $value )    {
    error_log( $key  . ' => ' $value );
}
foreach ( get_object_vars($event->data->object['metadata']) as $key => $value) {
    error_log( $key  . ' => ' $value );
}

is there something glaringly obvious i am missing?

6
  • Assume you're talking about github.com/stripe/stripe-php/blob/master/lib/StripeObject.php ? Commented Nov 6 at 16:03
  • 1
    Stripe objects implement the ArrayAccess protocol, which allows indexing them. But they're not really arrays, so you can't use all array operations like foreach. Commented Nov 6 at 16:07
  • 1
    @Barmar you can foreach over the public properties of a PHP class in general (3v4l.org/fKXB1) so it's a bit unclear (to me at least) why the properties of this particular StripeObect class would be any different? What have I missed? Commented Nov 6 at 16:09
  • @ADyson ISTR that they're private or protected properties. So it would need to implement the Iterable interface. Commented Nov 6 at 16:11
  • 1
    @ADyson OK. I knew var_dump() distinguishes them, I couldn't remember if print_r() does (I hadn't looked at your demo). Commented Nov 6 at 16:14

1 Answer 1

3

The comments are correct and the reason it doesn't work is that metadata is a StripeObject defined here which implements Countable so you can count the keys but it doesn't implement IteratorAggregate at the moment. What you need to do is convert this to an array first which then works

$customer = $stripe->customers->create([
  'metadata' => [
    'key1' => 'value1',
    'key2' => 'value2',
  ],
]);

foreach($customer->metadata->toArray() as $key => $value) {
  error_log( $key  . ' => ' $value );
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you. very similar question/answer here: stackoverflow.com/questions/63144905/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.