The for loop is supposed to loop 10 times.
When I just print the $fname
, $lname
and $email
I am getting the 10 data (Working fine)
But I need to store those value in an associative array.
$assocDataArray = array();
foreach ($data as $key => $value) {
foreach ($value['emails'] as $lead) {
$fname = $lead['first_name'];
$lname = $lead['last_name'];
$email = $lead['value'];
//put objects in array
$assocDataArray += [ "First Name" => $fname ];
$assocDataArray += [ "Last Name" => $lname ];
$assocDataArray += [ "Email" => $email ];
}
}
var_dump($assocDataArray);
var_dump
is only giving me one array [Expected 10]
This assignment:-
$assocDataArray += [ "First Name" => $fname ];
$assocDataArray += [ "Last Name" => $lname ];
$assocDataArray += [ "Email" => $email ];
Is wrong.Change it like below
$assocDataArray[] = [ "First Name" => $fname ,"Last Name" => $lname ,"Email" => $email ];
You can assign the key
$assocDataArray = array();
foreach ($data as $key => $value) {
foreach ($value['emails'] as $lead) {
$fname = $lead['first_name'];
$lname = $lead['last_name'];
$email = $lead['value'];
//put objects in array
$assocDataArray[ $key ]['First Name'] = $fname;
$assocDataArray[ $key ]['Last Name'] = $lname;
$assocDataArray[ $key ]['Email'] = $email;
}
}
var_dump($assocDataArray);
```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With