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 '[email protected]' (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.
$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.
CodePudding user response:
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'];
CodePudding user response:
You loop over the levels you dont know and then use the data from the inner level
foreach ( $array as $level1){
foreach ( $level1 as $name => $val ){
printf('Field name is %s and the value is %s', $name, $val);
}
}
CodePudding user response:
<?php
$response = array(
96 => array (
'customerName' => 'ClutchPoints, Inc.',
'email' => '[email protected]',
'additionalEmails' => null,
'addressStreet' =>'11390 W. Olympic Blvd. #420',
'addressLine2' => null,
'addressCity' => 'Los Angeles',
'addressState' => 'CA',
'addressPostalCode' => '90064',
'addressCountry' => 'US',
'companyName' => 'ClutchPoints, Inc.',
),
);
var_dump($response);
foreach ($response as $values) {
foreach ($values as $label => $value) {
echo $label; //customerName
echo $value; //ClutchPoints, Inc.
}
}
Demo - https://3v4l.org/hqgkB