1
stdClass Object ( 
[id] => 8586332 
[email] => [email protected] 
[optInType] => Unknown 
[emailType] => Html 
[dataFields] => Array 
( 
[0] => stdClass Object ( [key] => FIRSTNAME [value] => Bill ) 
[1] => stdClass Object ( [key] => FULLNAME [value] => Bill Jones ) 
[2] => stdClass Object ( [key] => GENDER [value] => ) 
[3] => stdClass Object ( [key] => LASTNAME [value] => Jones ) 
[4] => stdClass Object ( [key] => LASTSUBSCRIBED [value] => 2019-12-20T21:13:20.359947Z ) 
[5] => stdClass Object ( [key] => POSTCODE [value] => ) 
[6] => stdClass Object ( [key] => THIS_KEY [value] => This Value ) 
) 
[status] => Subscribed )

I have this JSON object and array in PHP that I have decoded.

I am trying to access 'This Value' but I am stuck.

I also need to find a way to do it where I don't know if it will always be number 6.

I have made these attempts:

$object->{"dataFields[1]"};

and

$object['dataFields'][6]['THIS_KEY'];

I can access the email like this:

echo $objec[1]->email;
2
  • Please always present your array/object data as var_export() output or a json string so that volunteers can more easily set up there testing scripts. Commented Dec 21, 2019 at 22:00
  • Cast the objects of the dataFields array into a custom class. Then you can access it more easily, or if you prefer not to, refer to mickmackusa's answer. Commented Dec 21, 2019 at 22:30

1 Answer 1

2

You will need to search through the subarray for the qualifying object using the key property's value.

There will be functional techniques to do this, but I'll show a simple loop and break technique.

Code: (Demo)

$object = (object) [
    'id'=> 8586332,
    'email' => '[email protected]',
    'optInType' => 'Unknown',
    'emailType' => 'Html',
    'dataFields' => [
        (object)['key' => 'FIRSTNAME', 'value' => 'Bill'],
        (object)['key' => 'FULLNAME', 'value' => 'Tom Jones'],
        (object)['key' => 'GENDER', 'value' => ''],
        (object)['key' => 'LASTNAME', 'value' => 'Jones'],
        (object)['key' => 'LASTSUBSCRIBED', 'value' => '2019-12-20T21:13:20.359947Z'],
        (object)['key' => 'POSTCODE', 'value' => ''],
        (object)['key' => 'THIS_KEY', 'value' => 'This Value'],
    ],
    'status' => 'Subscribed'
];

foreach ($object->dataFields as $dataRow) {
    if ($dataRow->key === 'THIS_KEY') {
        echo $dataRow->value;
        break;
    }
}

Output:

This Value

A functional search can look like this: (Demo)

$index = array_search('THIS_KEY', array_column($object->dataFields, 'key'));
if ($index !== false) {
    echo $object->dataFields[$index]->value; // same output as above
}

If you want simpler access to multiple values in the subarray, then assign temporary keys: (Demo)

$assocDataFields = array_column($object->dataFields, null, 'key');

echo $assocDataFields['THIS_KEY']->value;
echo "\n";
echo $assocDataFields['FULLNAME']->value;

Output:

This Value
Tom Jones
Sign up to request clarification or add additional context in comments.

Comments

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.