arkitektron
arkitektron

Reputation: 81

Getting PHP variables inside of foreach loop from JSON

Please review the following code I am using:

<?php

$head = '[ { "status": "active", "confirmed_opt_in": null, "account_id": 1738888, "fields": { "first_name": "Ash", "last_name": "Wright" }, "member_id": 262268888, "last_modified_at": "@D:2016-12-30T19:58:25", "member_status_id": "a", "plaintext_preferred": false, "email_error": null, "member_since": "@D:2014-05-05T16:01:21", "bounce_count": 0, "deleted_at": null, "email": "[email protected]" } ]';

$data = json_decode($head,true); // the second param will cast it as array, default is obejct. See the linked docs of json_decode!
foreach($data as $item) {
    $id=$item['member_id'];
    $email=$item['email'];
    echo "ID: $id EMAIL: $email<br/>";
    // put your code here to write into DB or whatever you wanna do!
}

?>

In addition to the $id and $email variables I would also like to capture the first name and last name from the above JSON return in order to have variables for the first name and last name. Could someone please so me the syntax to accomplish this?

Upvotes: 0

Views: 623

Answers (2)

Rahul Gandhi
Rahul Gandhi

Reputation: 19

You Should try two thinging, here is muliple array 1) decode json

Array

( [0] => Array ( [status] => active [confirmed_opt_in] => [account_id] => 1738888 [fields] => Array ( [first_name] => Ash [last_name] => Wright )

        [member_id] => 262268888
        [last_modified_at] => @D:2016-12-30T19:58:25
        [member_status_id] => a
        [plaintext_preferred] => 
        [email_error] => 
        [member_since] => @D:2014-05-05T16:01:21
        [bounce_count] => 0
        [deleted_at] => 
        [email] => [email protected]
    )

)

Use for loop or foreach loop for getting mulitple array values

foreach ($data as $key => $value) {
   $first_name = $value['fields']['first_name'];
   $last_name = $value['fields']['last_name'];  
}

you should print first_name and last_name

echo "Name : ".$first_name." ".$last_name;

2) Use extract() in foreach loop

foreach ($data as $key => $value) {
    extract($value);
    $first_name = $fields['first_name'];
    $last_name = $fields['last_name'];
}

print first_name and last_name

echo "Name : ".$first_name." ".$last_name;

If you need to print directly first_name and last_name then you should try this one.

foreach ($data as $key => $value) {
    extract($value);
    extract($fields);
    echo "Name : ".$first_name." ".$last_name;  
}

Upvotes: 0

Indrasis Datta
Indrasis Datta

Reputation: 8606

You should print_r your json decoded array. The first and last names are within the "fields" key.

echo "<pre>";
print_r($data);

Array:

Array
(
  [0] => Array
    (
        [status] => active
        [confirmed_opt_in] => 
        [account_id] => 1738888
        [fields] => Array
            (
                [first_name] => Ash
                [last_name] => Wright
            )

        [member_id] => 262268888
        [last_modified_at] => @D:2016-12-30T19:58:25
        [member_status_id] => a
        [plaintext_preferred] => 
        [email_error] => 
        [member_since] => @D:2014-05-05T16:01:21
        [bounce_count] => 0
        [deleted_at] => 
        [email] => [email protected]
    )

 )

Solution:

$first_name = $item['fields']['first_name'];
$last_name = $item['fields']['last_name'];

echo "Name : ". $first_name . ' '.$last_name;

Upvotes: 1

Related Questions