Solved

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

Posted on 2013-06-30
4
429 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 109

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 109

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

809 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