To be fair, I'm unsure if an Array Push is the best method for what I'm trying to accomplish, considering it works in Part B of the script.
I have a ForEach function that creates two (2) arrays with data. The array $ee2[] works as intended (it pulls data from DB, and doesn't duplicate the data). On the contrary, $eRD[] seems to only "append" the data to the array rather than "reserving" the array per iteration.
Is it better to use something else? I researched other articles and didn't see a fit. I also researched how to use objects instead-but pretty much broke everything. Any suggestions?
- I've tried declaring $eRD[] as an array above the forEach loop - No success, only appends.
- I've tried moving $ee2[] inside of $eRD[]'s forEach loop - it made things wonky.
- I've tried re-structuring the data's output using "separate" variables such as $eRD1, $eRD2.. etc but that seemed to be such a rigorous process.
- I've tried $eRD[$rD->name] which seems to only grab the "first" instance and uses it globally. $rD->name is unique to each iteration.
The $eRD[] forEach finds an associated title and answer for the data it finds.
$quickQues = new QUICKQUES\Init();
if(!empty($engs)){
foreach($engs as $e){
$rawData = json_decode($e['rawSubmittedData']);
$rawD = $rawData->data;
foreach($rawD as $rD){
$findModule = $quickQues->thisElementByID($rD->name);
if(!empty($findModule)){
$moduleTitle = json_decode($findModule['pqfeElement']);
$moduleTitleA = $moduleTitle->qeTitle;
$eRD[] = array(
'name'=>$moduleTitleA,
'value'=>$rD->value
);
$moduleTitle = "";
$moduleTitleA = "";
}else{
}
}
$ee2[] = array(
'eSPY'=>$e['eSPY'],
'submissionElementType'=>$e['submissionElementType'],
'submittedBy'=>$e['submittedBy'],
'rawSubmittedData'=>$eRD,
'isInternalForm'=>$e['isInternalForm'],
'eSpyStatus'=>$e['eSpyStatus'],
'lastUpdated'=>$e['lastUpdated'],
'formName'=>$e['formName']
);
}
}
echo json_encode(
array(
array(
'response'=>'hFetch',
),
array(
'engagements'=>array(
'total'=>$count,
'info'=>$ee2,
'cEE'=>count($ee2)
),
) ) );
After getting help, this method below seems to work perfectly for me. Hope this helps someone else in the future..
if (!isset($eRD[$rD->name])) {
$eRD[$rD->name]['items'] = [];
}
$eRD[$rD->name]['items'][] = array(
'name'=>$moduleTitleA,
'value'=>$rD->value
);