Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extracting values from an array in PHP

From a function we use, it returns an array like this.

array (size=1)
  96 => 
    array (size=10)
      'customerName' => string 'ClutchPoints, Inc.' (length=18)
      'email' => string 'nish@clutchpoints.com' (length=21)
      'additionalEmails' => null
      'addressStreet' => string '11390 W. Olympic Blvd. #420' (length=27)
      'addressLine2' => null
      'addressCity' => string 'Los Angeles' (length=11)
      'addressState' => string 'CA' (length=2)
      'addressPostalCode' => string '90064' (length=5)
      'addressCountry' => string 'US' (length=2)
      'companyName' => string 'ClutchPoints, Inc.' (length=18)

I cannot figure out a way to get the inside array’s value to print in the applications. Is there any way to get the values out?

Thank you in Advanced.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$data = array_map(function ( $obj ){
        return array(
            'customerName' => $obj->name,
            'email' => $obj->company_contact->email,
            'additionalEmails' => $obj->company_contact->secondaryContactEmail,
            'addressStreet' => $obj->company_contact->addressLine1,
            'addressLine2' => $obj->company_contact->addressLine2,
            'addressCity' => $obj->company_contact->city,
            'addressState' => $obj->company_contact->stateOrProvince,
            'addressPostalCode' => $obj->company_contact->zipOrPostalCode,
            'addressCountry' => $obj->company_contact->country,
            'companyName' => $obj->name
        );
    }, $customer_data);

I tried using array_map

it also returns an array with a different index number.

>Solution :

Since it’s always only one element, you can use current to get first element of array, no matter of its key:

$element = current($data); // ALTERNATIVE: array_shift($data);

echo $element['additionalEmails'];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading