I have a post call that returns JSON in one of two ways:
$json1 = '{"found":1,"email":"[email protected]","error":"","rd":"[email protected]"}';
$json2 = '{"found":1,"email":"[email protected],[email protected]","error":"","rd":"[email protected],[email protected]"}';
In the first, the email and rd parameters each only have one email address. In the second, those same two parameters have multiple recipients each.
I need to take the emails from each parameter and add it to an array that already exists:
$recipients = array('[email protected]');
I can get it to work with the $json1 variable using the following code:
array_push($recipients, $obj->{'rd'}, $obj->{'email'});
The second JSON option is posted more rarely, but I still need the same code to work for both instances. And currently, if I use the above code with the second JSON data it returns this:
Array
(
[0] => [email protected]
[1] => [email protected],[email protected]
[2] => [email protected],[email protected]
)
Which has multiple emails in the same parameter. Does anyone have any insight on how I can separate the emails within each array item?
A working example:
http://sandbox.onlinephpfunctions.com/code/24c4a1eaea98566b65cd36e221dd1f185e820ea6
explodeitems by ','assoc flag = truelike thisjson_decode($json_string, TRUE);so that your objects are converted to arrays and you can work with syntax like$obj['email']rather than$obj->{'email'}