1

how can I access the values in a nested array, e.g. when using get for contact entity e.g. how can I adjust the following to get the first name? (not working for me at the moment)

$result = civicrm_api3('Contact', 'get', array(
   'sequential' => 1,
   'return' => array("first_name", "last_name", "email"),
   'id' => 28296
));
echo $result['values'][0]['first_name'];

if I use var_dump ($result) I get the following so the results are there:

array(5) { ["is_error"]=> int(0) ["version"]=> int(3) ["count"]=> int(1) ["id"]=> int(28296) ["values"]=> array(1) { [0]=> array(6) { ["contact_id"]=> string(5) "28296" ["first_name"]=> string(8) "Thirteen" ["last_name"]=> string(4) "Test" ["email_id"]=> string(5) "27750" ["email"]=> string(25) "thirteen.test@artigma.com" ["id"]=> string(5) "28296" } } }

thanks much!

1

You could just use something like $contactData = $result['values'][0] for your var_dump to test. Your echo should be right, in theory.

  • Also, getSingle may be useful to you: $result = civicrm_api3('Contact', 'getsingle', array( 'sequential' => 1, 'last_name' => "Tobias", 'first_name' => "Randy", 'id' => 4, )); – Randy Tobias Apr 20 '17 at 18:24
  • thanks for pointing me in the right direction, guys. This did the trick: $contactData = $result['values'][0]['contact_id']; echo "Contact ID: " . $contactData; – MtnPavlas Apr 21 '17 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.