Solved

How to loop through this kind of an array returned from mailchimp.

Posted on 2013-06-30
4
430 Views
Last Modified: 2013-07-02
Hi,
Using the mailchimp API I'm returning the array below. It contains the email addresses and their open counts for a particular email campaign. I'm having a hard time trying to loop through it so I can insert each email and corresponding open count into my db. Can someone show me an example of looping through this thing to do an insert?

array(2) { [0]=> array(2) { ["email"]=> string(24) "john@mysite.com" ["open_count"]=> int(1) } [1]=> array(2) { ["email"]=> string(23) "bill@mydomain.com" ["open_count"]=> int(3) } } 

Open in new window


Thanks as always,
tj
0
Comment
Question by:tjyoung
  • 2
  • 2
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39288752
This appears to be an array of arrays.  Iterate over the top array to get each "sub-array" by number.  Iterate over each "sub-array" to get the elements named "email" and "open_count".

array(2) 
{ [0]=> array(2) 
  {  ["email"]=> string(24) "john@mysite.com" 
     ["open_count"]=> int(1)
  } 
  [1]=> array(2) 
  {   ["email"]=> string(23) "bill@mydomain.com" 
      ["open_count"]=> int(3) 
  }
} 

Open in new window

0
 
LVL 1

Author Comment

by:tjyoung
ID: 39293505
Hi,
I've tried hundred variations only getting stuff like: arrayarray and a myriad of other mistakes. I'd post my attempts but none of them seemed close or simply failed.

cleaning up the arrays I appreciate, should've done that posting my question.

Can you give me an example of iterating through this? I can figure out adding to a DB I should think but I can't for the life of me get to the correct data.

Its a bit of a grey area in my 'home schooling' efforts.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39293606
Some good learning resources here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

In PHP when you echo an array, PHP rather unhelpfully prints "array."  See if this code sample helps get you closer

Please see http://www.laprbass.com/RAY_temp_tjyoung.php

<?php // RAY_temp_tjyoung.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28171952.html

/* DATA STRUCTURE
array(2)
{ [0]=> array(2)
  {  ["email"]=> string(24) "john@mysite.com"
     ["open_count"]=> int(1)
  }
  [1]=> array(2)
  {   ["email"]=> string(23) "bill@mydomain.com"
      ["open_count"]=> int(3)
  }
}
*/

// CREATE THE TEST DATA FROM THE DESCRIPTION
$arr = array
( 0 => array
       ( 'email'      => 'john@mysite.com'
       , 'open_count' => 1
       )
, 1 => array
       ( 'email'      => 'bill@mydomain.com'
       , 'open_count' => 3
       )
)
;

// DOES IT LOOK RIGHT? (YES)
var_dump($arr);

// ITERATE OVER THE ARRAY AND SUB-ARRAYS
foreach ($arr as $key => $sub)
{
    echo PHP_EOL . "MAIN ARRAY KEY: $key";
    foreach ($sub as $key => $val)
    {
        echo PHP_EOL . "   SUB ARRAY KEY: $key VALUE: $val";
    }
    echo PHP_EOL;
}

Open in new window

Best, ~Ray
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 39293732
Thank you very much. Makes perfect sense once you see it...
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Giant ibd file for our biggest table on mysql 2 23
Formating field in mysql Advance formatting 1 38
What is this? I've never seen this... 2 28
xml with php question 5 30
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question