0

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?

  1. I've tried declaring $eRD[] as an array above the forEach loop - No success, only appends.
  2. I've tried moving $ee2[] inside of $eRD[]'s forEach loop - it made things wonky.
  3. 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.
  4. 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
                                );
Racer
  • 63
  • 7
  • As I said under the answer, `if (!isset($eRD[$rD->name])) { $eRD[$rD->name]['items'] = []; }` is not necessary. Just remove it. – mickmackusa Nov 29 '21 at 01:04

1 Answers1

1

If $rD->name is a unique key, you can use it as an index into the array

$eRD[$rD->name] = ...

If your goal is to have an array of items under each $eRD element, you could do something like

if (!isset($eRD[$rD->name])) {
    $eRD[$rD->name]['items'] = [];
}
$eRD[$rD->name]['items'][] = $rD->value;
user2182349
  • 9,569
  • 3
  • 29
  • 41
  • i tried that earlier, using $eRD[$rD->name] seems to only take that "one" instance and uses it everywhere where as using $eRD[] pulls all the different data sets but appends it rather than reserving it – Racer Nov 29 '21 at 00:56
  • will try your second comment. thanks in advance! – Racer Nov 29 '21 at 00:56
  • This page is completely redundant. Exact same advice is already given on Stack Overflow here: https://stackoverflow.com/questions/46364515/appending-elements-to-array-and-skipping-if-some-value-existsphp Also, it is not necessary at all to pre-declare the parent array before pushing a child into it in php `if (!isset($eRD[$rD->name])) { $eRD[$rD->name]['items'] = []; }` can be safely removed. – mickmackusa Nov 29 '21 at 01:00
  • The first suggestion worked perfectly for me. Someone may have misunderstood the goal, however with the submitted feedback above I was able to resolve my issue. Hopefully, my updated question resolves issues for anyone else... FYI, the shared links do not resolve the question at hand. Those methods were tried earlier. – Racer Nov 29 '21 at 01:04
  • @Racer, the shared link tells you to use a temporary key when pushing new values into your result array -- just as this answer does. This other answer demonstrates the same basic technique and explains very well how it works: https://stackoverflow.com/a/35334425/2943403 – mickmackusa Nov 29 '21 at 01:06