2

I have this json array without id and variable email number:

Array contains multiple email separated by a comma:

[{"email":"[email protected]","timestamp":1516820089,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"processed","category":"cat facts","sg_event_id":"FDl9L7nP4zJXgLYUGhqqoA==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"},
 {"email":"[email protected]","timestamp":1516820089,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"deferred","category":"cat facts","sg_event_id":"jbtTInm9Pej80gzRXRg9Pg==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","response":"400 try again later","attempt":"5"},
 {"email":"[email protected]","timestamp":1516820089,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"delivered","category":"cat facts","sg_event_id":"XUBvHRS8T0eD_Eea8IECDA==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","response":"250 OK"},
 {"email":"[email protected]","timestamp":1516820089,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"open","category":"cat facts","sg_event_id":"Wp2888u5ZaJX7Pd-_DbFpA==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255"},
 {"email":"[email protected]","timestamp":1516820089,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"click","category":"cat facts","sg_event_id":"7iXIm9S5SAuhRmoy4QIZZg==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255","url":"http://www.sendgrid.com/"},
 {"email":"[email protected]","timestamp":1516820089,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"bounce","category":"cat facts","sg_event_id":"3HJSeIIA2FzBjQxXvlf2hQ==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","reason":"500 unknown recipient","status":"5.0.0"},
 {"email":"[email protected]","timestamp":1516820089,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"dropped","category":"cat facts","sg_event_id":"8acL8P-wxlb0CJoxBEjYuQ==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","reason":"Bounced Address","status":"5.0.0"},
 {"email":"[email protected]","timestamp":1516820089,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"spamreport","category":"cat facts","sg_event_id":"cgn6ggkYz1S7XT5D5nRo9w==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"},
 {"email":"[email protected]","timestamp":1516820089,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"unsubscribe","category":"cat facts","sg_event_id":"suFV8ITF-9ND3j1SrlW-fA==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"},
 {"email":"[email protected]","timestamp":1516820089,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"group_unsubscribe","category":"cat facts","sg_event_id":"KljxrqIWTvF0mEshFKnw1Q==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255","url":"http://www.sendgrid.com/","asm_group_id":10},
 {"email":"[email protected]","timestamp":1516820089,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"group_resubscribe","category":"cat facts","sg_event_id":"F7oTvsqxOIQyRWyLJeXIjA==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255","url":"http://www.sendgrid.com/","asm_group_id":10}]

And i need to create a while/loop for getting separate email data, i have also tried with

$obj = json_decode( file_get_contents( 'php://input' ) );
foreach ($obj as $value) {    
   $value->email;
}

but it return always and only the first or last value

6
  • How are you trying to output or store this list of emails? Commented Jan 24, 2018 at 19:24
  • It is a webhooks of sendgrid =) sendgrid.com/docs/API_Reference/Webhooks/index.html Commented Jan 24, 2018 at 19:25
  • Did you check if $obj has the expected content? I just checked the code without getting the data from input and it works. Commented Jan 24, 2018 at 19:28
  • Really, code for insert in mysql with pdo is incorrect for while, i have fixed and my code work =) Commented Jan 24, 2018 at 19:37
  • All of the email addresses in email are [email protected] so how do you know it's the first or last one??? Commented Jan 24, 2018 at 19:45

2 Answers 2

2

This should work for you if you know your array keys, and it eliminates the double loop.

$array = json_decode(file_get_contents( 'php://input' ), true);
foreach($array as $innerArray){
    $emails[] = $innerArray['email'];
    //or do whatever you want with each email as $innerArray['email'], repeat for each key.
}
Sign up to request clarification or add additional context in comments.

Comments

2

you can try like this:

$obj = json_decode( file_get_contents( 'php://input' ) );
foreach ($obj as $item) {
   $props = get_object_vars($item);
   foreach ($props as $key => $value) {
       /*do what you like with $key and $value */
   }
}

3 Comments

Looks good, but why not just use the true option in json_decode and get an array > an object to start with?
you'd still have to do 2 loops, but you're right, i just assumed he wanted objects from his code
Oh wow. Just reread the question - I thought he only wanted the email addresses. Would probably be better to have keys too.

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.