John Davidson

php - Looping trough a protected SendinBlue object


i have succesfully integrated the SendinBlue v3 API into my codeigniter application, but for some reason i'm unable to loop trough the results when using the API.

When i var_dump, my object looks like this :

 object(SendinBlue\Client\Model\GetEmailCampaigns)#110 (1) {
array(2) {
array(2) {
array(23) {
string(12) "Testcampagne"

Since the root object is protected , i'm not able to loop trough this to see my campaigns.
I was able to read it by converting it to an array , $result = (array)$result;
and then i had to use

foreach ($result as $campaigns)
foreach ($campaigns as $campaignh)
foreach ((array)$campaignh as $campaign)
echo $campaign['status'];

I'm sure there must be a way to loop trough the campaigns in one single foreach? Feeling really stupid about this!

Thank you!

My Answer:
To loop through the results of a protected SendinBlue object in PHP, you can use the `getIterator()` method to access the data within the object. Here's an example of how you can loop through the results:

// Assuming $emailCampaigns is the object of type SendinBlue\Client\Model\GetEmailCampaigns

// Get the iterator for the object
$iterator = $emailCampaigns->getIterator();

// Loop through the results
foreach ($iterator as $campaign) {
// Access the properties of the campaign object
echo $campaign->getId() . ' - ' . $campaign->getName() . '

In this example, we are using the `getIterator()` method to get an iterator for the object, which allows us to loop through the results. Inside the loop, we can access the properties of each campaign object using the appropriate getter methods (e.g. `getId()` and `getName()`).

Make sure to replace `getId()` and `getName()` with the actual getter methods for the properties you want to access in the `SendinBlue\Client\Model\GetEmailCampaigns` class.

Rate this post

3 of 5 based on 2253 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us